In continuation of my previous blog on the basic JavaScript expressions, this blog is about Falsy and Truthy values in JavaScript.
When presented with a non boolean expression instead of a boolean value, JavaScript uses Truthy or Falsy values instead to determine the result. At first glance, this could be confusing and could lead to errors while scripting.
The Falsy Values
All expressions, objects and values are Truthy by default. There are exceptions and they are called the Falsy values and are listed below:
# The empty string; ""
# null
# undefined
# The number 0
# NaN
# The boolean false
It gets tricky
If you see the below table, you will note that it gets tricky or rather one should be a little extra careful while dealing with Falsy values.
To remember:
# An empty array evaluates to Truthy whereas any boolean operation on an empty array results in a Falsy
# NaN is not equal to anything, not even itself
# Undefined is not equal to anything, not even itself
# Use strict equals (===) to be extra sure while comparing. This compares by type and value
# Avoid using Falsy value and stick to plain true or false to be sure of the results.
Now that you have read this far, Congratulations! I expect 0 bugs from your code henceforth :)
When presented with a non boolean expression instead of a boolean value, JavaScript uses Truthy or Falsy values instead to determine the result. At first glance, this could be confusing and could lead to errors while scripting.
var a = "someval";
var b;
if (a) { // this is a truthy
// Block is executed
}
if (b) { // b is undefined and so a falsy
// Block is not executed
}
The Falsy Values
All expressions, objects and values are Truthy by default. There are exceptions and they are called the Falsy values and are listed below:
# The empty string; ""
# null
# undefined
# The number 0
# NaN
# The boolean false
It gets tricky
If you see the below table, you will note that it gets tricky or rather one should be a little extra careful while dealing with Falsy values.
Expression | Value |
---|---|
Falsy Values | |
false | false |
NaN | false |
undefined | false |
0 | false |
"" | false |
null | false |
Truthy Values | |
Infinity | true |
true | true |
1 | true |
"1" | true |
"0" | true |
Complicating things now | |
0+0 | false |
0+"0" | true |
"0"+"0" | true |
1-1 | false |
"1"-1 | false |
"1"-"1" | false |
Double Negation | |
!!0 | false |
!!1 | true |
!!"0" | true |
!!"1" | true |
Logical comparison | |
null == null | true |
null == undefined | true |
null == NaN | false |
null == "" | false |
null == 0 | false |
null == false | false |
undefined == undefined | false |
undefined == NaN | false |
undefined == "" | false |
undefined == 0 | false |
undefined == false | false |
NaN == NaN | false |
NaN == "" | false |
NaN == 0 | false |
NaN == false | false |
"" == "" | true |
"" == 0 | true |
"" == false | true |
0 == 0 | true |
0 == false | true |
false == false | true |
Strict Logical comparison | |
null === null | true |
"" === "" | true |
"" === 0 | false |
"" === false | false |
0 === 0 | true |
0 === false | false |
false === false | true |
Arrays | |
[ ] | true |
![ ] | false |
[ ] == true | false |
To remember:
# An empty array evaluates to Truthy whereas any boolean operation on an empty array results in a Falsy
# NaN is not equal to anything, not even itself
# Undefined is not equal to anything, not even itself
# Use strict equals (===) to be extra sure while comparing. This compares by type and value
# Avoid using Falsy value and stick to plain true or false to be sure of the results.
Now that you have read this far, Congratulations! I expect 0 bugs from your code henceforth :)
thanks for the comment Joe .. and thanks for the link too
ReplyDelete