Can someone please explain JavaScript Truthy and Falsy, using the below sample data. I have read other threads but still confused.
var a = 0;
var a = 10 == 5;
var a = 1;
var a = -1;
From my understanding, I believe that var a = 1;
is the only truthy and the rest are falsy - is this correct?
There's a simple way to check, which you can use now and forever:
To wit:
Also see a list of all falsey values in JavaScript.
No.
Number zero is falsy. However, note that the string zero
"0"
is truthy.This is same as
var a = (10 == 5);
, so this is falsy.Any non-zero number including negative numbers is truthy.
Quoting from MDN
List of falsy values in JavaScript:From MDN
false
null
undefined
0
NaN
''
document.all
Truthy -> Value that resolve to true in boolean context
Falsy -> Value that resolve to false in boolean context
For better understanding,
falsy
values is given below.false
0
empty
string
null
undefined
NaN