I've found myself running into the same buggy thing over and over the last couple of weeks, it's when using the ||
operator to assign a default value if the first value isn't set:
(myVariable || 'somestring')
This works whenever myVariable
isn't a 0
, but if it is a 0
then it bees problematic since it will then count it as a falsy value. This can be solved by checking if it is a zero, but it quickly bees an unreadable mess such as (myVariable >= 0 ? myVariable : 'somestring')
.
What would be the easiest and most correct way of allowing myVariable
to be 0
but still count as the truthy value? Note that myVariable
must still be the original value so using the !!
operator for example won't work.
I've found myself running into the same buggy thing over and over the last couple of weeks, it's when using the ||
operator to assign a default value if the first value isn't set:
(myVariable || 'somestring')
This works whenever myVariable
isn't a 0
, but if it is a 0
then it bees problematic since it will then count it as a falsy value. This can be solved by checking if it is a zero, but it quickly bees an unreadable mess such as (myVariable >= 0 ? myVariable : 'somestring')
.
What would be the easiest and most correct way of allowing myVariable
to be 0
but still count as the truthy value? Note that myVariable
must still be the original value so using the !!
operator for example won't work.
-
What else do you consider to be a
truthy
value? – VisioN Commented Apr 21, 2016 at 11:39 - @RayonDabre You didn't understand the question. Your code will always return a boolean rather than the actual variable if it's not false. – VisioN Commented Apr 21, 2016 at 11:40
- @VisioN What do you mean? – Chrillewoodz Commented Apr 21, 2016 at 11:41
-
If you consider
0
to betruthy
, what otherfalthy
values do you consider to betruthy
as well? Or0
is the only exception? – VisioN Commented Apr 21, 2016 at 11:43 - @VisioN 0 is the only exception, which always cause bugs in my code. – Chrillewoodz Commented Apr 21, 2016 at 11:43
4 Answers
Reset to default 3A solution with logical operators only:
function getValue(v) {
return v !== 0 && (v || 'someString') || 0;
}
document.write(getValue(null) + '<br>');
document.write(getValue(false) + '<br>');
document.write(getValue(undefined) + '<br>');
document.write(getValue(0) + '<br>');
document.write(getValue(1) + '<br>');
document.write(getValue('42') + '<br>');
Another proposal
function getValue(v) {
return v || v === 0 ? v : 'someString';
}
document.write(getValue(null) + '<br>');
document.write(getValue(false) + '<br>');
document.write(getValue(undefined) + '<br>');
document.write(getValue(0) + '<br>');
document.write(getValue(1) + '<br>');
document.write(getValue('42') + '<br>');
My solution:
(myVariable.toString() || 'somestring')
You'll need a try/catch though
Try this:
x === 0 || x ? x : y
Gives same output as:
x !== 0 && (x || y) || 0;
but is easier to read I think
i found easy solution:
( myVariable === 0 ? '0' : ( myVariable || 'somestring' ) )