JS: falsy expressions | JavaScript
JavaScript falsy expressions
JavaScript falsy expression are those expressions, result as a false
boolean value in if
condition and ternary ?:
operator. Rest of the expression return true.
Falsy Expression means an expression returns false boolean value when prided inif
condition or ternary operators?:
false is falsy
false
is obviously falsy in if
condition and ternary operator ?:
:
var x = false;
if(x)
alert('truly');
else
alert('falsy')
falsy
Comparing two false variables
Comparing a variable itself or with another variable having false
values, is truly
var x = false;
var y = false;
if(x == y)
alert('truly');
else
alert('falsy')
truly
Comparing two false hard coded
Comparing a variable itself or with another variable having false
values, is truly
if(false == false)
alert('truly');
else
alert('falsy')
truly
Empty string "" is false
Empty string ""
is treated as false
in if
condition and ternary operator ?:
:
var x = "";
if(x)
alert('truly');
else
alert('falsy')
falsy
Comparing two Empty string "" variables
Comparing a variable itself or with another variable having empty ""
values, is truly
var x = "";
var y = "";
if(x == y)
alert('truly');
else
alert('falsy')
truly
Comparing two Empty string hard coded
Comparing a variable itself or with another variable having empty string ""
values, is truly
if("" == "")
alert('truly');
else
alert('falsy')
truly
Zero 0 is falsy
Zero 0
is treated as false
in if
condition and ternary operator ?:
:
var x = 0;
if(x)
alert('truly');
else
alert('falsy')
falsy
Comparing two Zero 0 variables
Comparing a variable itself or with another variable having Zero 0
values, is truly
var x = 0;
var y = 0;
if(x == y)
alert('truly');
else
alert('falsy')
truly
undefined is false
undefined
is treated as false
in if
condition and ternary operator ?:
:
var x = undefined;
if(x)
alert('truly');
else
alert('falsy')
falsy
Comparing two undefined variables
Comparing a variable itself or with another variable having undefined
values, is truly
var x = undefined;
var y = undefined;
if(x == y)
alert('truly');
else
alert('falsy')
truly
Comparing two undefined hard coded
Comparing a variable itself or with another variable having undefined
values, is truly
if(undefined == undefined)
alert('truly');
else
alert('falsy')
truly
null is false
null
is treated as false
in if
condition and ternary operator ?:
:
var x = null;
if(x)
alert('truly');
else
alert('falsy')
falsy
Comparing two null variables
Comparing a variable itself or with another variable having null
values, is truly
var x = null;
var y = null;
if(x == y)
alert('truly');
else
alert('falsy')
truly
Comparing two null hard coded
Comparing a variable itself or with another variable having null
values, is truly
if(null == null)
alert('truly');
else
alert('falsy')
truly
NaN is false
NaN
or Number.NaN
is treated as false
in if
condition and ternary operator ?:
:
var x = NaN;
if(x)
alert('truly');
else
alert('falsy')
falsy
Comparing two NaN variables
Comparing a variable itself or with another variable having NaN
values, is falsy
var x = NaN;
var y = NaN;
if(x == y)
alert('truly');
else
alert('falsy')
falsy
Comparing two NaN hard coded
Comparing a variable itself or with another variable having NaN
values, is falsy
if(false == false)
alert('truly');
else
alert('falsy')
falsy
Empty Array [] is true
[]
is treated as true
in if
condition and ternary operator ?:
:
var x = [];
if(x)
alert('truly');
else
alert('falsy')
truly
Comparing two Empty array variables
Comparing a variable itself having []
values is truly
var x = [];
if(x == x)
alert('truly');
else
alert('falsy')
truly
Comparing a variable with another variable having []
values is falsy
var x = [];
var y = [];
if(x == y)
alert('truly');
else
alert('falsy')
falsy
Comparing two Empty array hard coded
Comparing a variable itself or with another variable having []
values, is falsy
if([] == [])
alert('truly');
else
alert('falsy')
truly
Empty object {} is true
Empty object {}
is treated as true
in if
condition and ternary operator ?:
:
var x = {};
if(x)
alert('truly');
else
alert('falsy')
truly
Comparing two Empty object variables
Comparing a variable itself having {}
values is truly
var x = {};
if(x == x)
alert('truly');
else
alert('falsy')
truly
Comparing a variable with another variable having {}
values is falsy
var x = {};
var y = {};
if(x == y)
alert('truly');
else
alert('falsy')
falsy
Comparing two Empty array hard coded
Comparing a variable itself or with another variable having []
values, is falsy
if([] == [])
alert('truly');
else
alert('falsy')
truly
In the the code snipped, we used double-equal sign==
instead of recommended tripple-equal sign===
to reveal that internal type-casting confusion, that doesn't apply for the falsy expression.
Falsy expressions as string are truly
Falsy expressions like false
, 0
, null
, undefined
, NaN
etc. are truly i.e. execute the true part of the if condition and ternary operator.
"false" is true
"false"
is treated as true
because it is a non-empty string
:
var x = "flase";
if(x)
alert('truly');
else
alert('falsy')
truly
"0" is true
"0"
is treated as true
because it is a non-empty string
:
var x = "0";
if(x)
alert('truly');
else
alert('falsy')
truly
"undefined" is true
"undefined"
is treated as true
because it is a non-empty string
:
var x = "undefined";
if(x)
alert('truly');
else
alert('falsy')
truly
"null" is true
"null"
is treated as true
because it is a non-empty string
:
var x = "null";
if(x)
alert('truly');
else
alert('falsy')
truly
"NaN" is true
"NaN"
is treated as true
because it is a non-empty string
:
var x = "0";
if(x)
alert('truly');
else
alert('falsy')
truly
As we have seen that "false"
, 0
, ""
, null
, undefined
, NaN
, NaN == NaN
execute the false
the false block of the if
condition and ternary operator ?:
hence they are falsy expressions.