Truthy and Falsy values in Javascript

iftekharalvee

Iftekharul Islam

Posted on May 8, 2021

Truthy and Falsy values in Javascript

We are going to discuss an important javascript topic for interviews that can help you also understand a basic of Javascript.

Truthy and Falsy values

In javascript when you declare a variable with some value except 0 Javascript should consider this as a true value and if the value is 0 Javascript will consider this as false.

const score = 20;

if(score){
console.log("It is true")
}
else{
console.log("It is false")
}
// Output : It is true
const duck = 0;

if(duck){
console.log("It is true")
}
else{
console.log("It is false")
}
// Output : It is false
Enter fullscreen mode Exit fullscreen mode

So Javascript will consider 0 as false and other values as true.

If you declare a string with its length < 0 then javascript will consider this as true, otherwise if you declare an empty string it will be considered as false.

const name1 = "Alvee";

if(name1){
console.log("It is true")
}
else{
console.log("It is false")
}
// Output : It is true
const name2 = "";

if(name2){
console.log("It is true")
}
else{
console.log("It is false")
}
// Output : It is false
Enter fullscreen mode Exit fullscreen mode

If you don't define a variable Javascript will consider it as false.

let name;

if(name){
console.log("It is true")
}
else{
console.log("It is false")
}
// Output : It is false
Enter fullscreen mode Exit fullscreen mode

If you define a variable as Null Javascript will consider it as false.

let name = null;

if(name){
console.log("It is true")
}
else{
console.log("It is false")
}
// Output : It is false
Enter fullscreen mode Exit fullscreen mode

If you also define a variable with NaN Javascript will consider it as false.

let value = NaN ;

if(value){
console.log("It is true")
}
else{
console.log("It is false")
}
// Output : It is false
Enter fullscreen mode Exit fullscreen mode

We saw that the empty string was false, but if you declare an empty array or object it will be considered as true value.

const array = [];

if(array){
console.log("It is true")
}
else{
console.log("It is false")
}
// Output : It is true

const object = {};

if(object){
console.log("It is true")
}
else{
console.log("It is false")
}
// Output : It is true
Enter fullscreen mode Exit fullscreen mode

Besides all these, if you declare a value as false, there what can Javascript do!

const value = false;

if(value){
console.log("It is true")
}
else{
console.log("It is false")
}
// Output : It is false
Enter fullscreen mode Exit fullscreen mode

So we can say that if you declare a variable with the value of undefined, null, NaN, 0, "" the output will be false.

The following values are always falsy:

  • false
  • 0 (zero)
  • '' or "" (empty string)
  • null
  • undefined
  • NaN

Everything else is truthy. That includes:

  • '0' (a string containing a single zero)
  • 'false' (a string containing the text “false”)
  • {} (an empty object)
  • function(){} (an “empty” function)

you can also check out this article I found informative

💖 💪 🙅 🚩
iftekharalvee
Iftekharul Islam

Posted on May 8, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related