π Welcome to Kasper Design! π
We are a team of web developers and designers who help you make money online. Find out how.
Call us for free on 07786050312
@Techrev_9999@JuliansBacke@_devJNS Indeed. It depends on the language and what you are trying to achieve. When debugging I would usually save the output of the ternary to a variable. Log the variable. And then return that variable. But I agree. Itβs janky and there are always trade offs.
@_devJNS In JavaScript I would write:
return 0 == a;
Since it is much safer and prevents mistakes like:
return (a = 0) ? true : false;
Slipping through the cracks. Which is an assignment not a comparison.
Also depending on the application I would even use a triple equals ===
@Techrev_9999@JuliansBacke@_devJNS Indeed. Depends on coding standards. Both are correct, as long as it's consistent throughout the codebase. Depends on which you find more readable.
@JuliansBacke@_devJNS JavaScript dev here. This is wrong. a == 0 loosely checks for equality with zero. !!a checks for truthiness. These produce different results for:
a = 0,
a = false,
a = 1
This will only work if a is the string "0".
I would even change option 2 to be:
return (0 == a) ? true : false