Javascript: Truthy And Falsy Values
Hey there! Welcome to the new year! I hope you smash your goals and every other thing you have set out to achieve.
In this article, we will read about Truthiness and Falsiness in JavaScript. All values have an inherent truthiness and falsiness to them. This concept comes into play mostly in logical operators and conditionals.
A falsy value is a value that is considered false
when encountered in a Boolean context. Below are examples of falsy values:
false
Nan
(Not a Number)undefined
null
0
" "
,' '
(an empty string)
In the same way, a truthy value is a value that is considered true
when encountered in a Boolean context. Here are some examples:
true
"hello"
(the string hello)1
[ ]
(an empty array){ }
(an empty object)
Using the if/else statements, you can check if a value is either truthy or falsy.
if(0) {
console.log("Truthy");
} else {
console.log("Falsy");
} //output will be Falsy
Subscribe to my newsletter
Read articles from Somtochukwu Nwosu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Somtochukwu Nwosu
Somtochukwu Nwosu
I'm passionate about learning and communicating that knowledge through my writing.