Check if falsy except zero in JavaScript
I am checking if a value is not falsy with
if (value) {
..
}
but I do want to accept zeros as a (non-falsy) value. How is this normally done? Would it be
if (value || value === 0) {
..
}
or what?
if (value || value === 0) {
..
}
is cleaner way, no problem.
Just for fun try this
val = 0;
if(val){
console.log(val)
}
//outputs 0 **update - it used to be.. but now it seems it doesnt in chrome
And
var val = 0;
if(val){
console.log(val)
}
//outputs nothing
I would use value || value === 0
. It's fine whichever way you do it but IMO this is the cleanest option.