Hey there, Grasshoppers. In JavaScript, the boolean can be either true
or false
. For example:
let x = 3;
if (x === 3) {
console.log("true!");
}
This will print the string "true!"
since x
is equal to 3.
But what if we have an if statement like this?
let x = 3;
if (x) {
console.log("true!");
}
This will also print "true!"
. Weird!
In JavaScript, there are 6 falsy values:
-
false
: The boolean type opposite totrue
. -
0
: The number 0. -
''
: A string with a length of 0 and has no characters. -
NaN
: Which means Not a Number, typically caused by some math problems. -
undefined
: A data type before it’s defined. -
null
: A data type that can be assigned to a blank space.
Any numbers other than 0, the string that is not empty, etc., is called truthy values. If you put these truthy values into an if
statement, then the if
statement will run.
Thanks!
P