Truthy and Falsy in JavaScript

In JavaScript, an expression must be evaluated as True or False. Generally, we called it as Truthy and Falsy in JavaScript. Let’s understand this with examples.

Falsy Expressions in JavaScript

1. False

If you pass false inside if statement, then it will always be evaluated as false.

Example:

 
if(false) {
    alert("This Statement is True");
}
else {
    alert("This Statement is False");
}
 

The output of the above code is “This Statement is False.” This will never change because else block is always going to execute when you run code.

2. 0 (ZERO)

JavaScript always evaluate value 0 as False in a condition.

Example:

 
if(0) {
    alert("This is NOT 0");
}
else {
    alert("This is 0");
}
 

The output of the above code is “This is 0“. This will never change because else block is always going to execute when you run code.

3. “” OR ” (Blank)

If you pass a blank value to the expression, then it will always be false.

Example:

 
if("") {
    alert("This is NOT Blank");
}
else {
    alert("This is Blank");
}
 


The output of the above code is “This is Blank.” This will never change because else block is always going to execute when you run code.

Note: Even if you try the above code with single quotes blank (”), then the output will be the same.

Note: Don’t give space (” ” OR ‘ ‘) between double or single quotes. Otherwise, it will evaluate as true because when you give space, it is not a blank value anymore. 

4. NULL

If you pass NULL to the expression, then it will always evaluate as false.

Example:

 
if(NULL) {
    alert("This is NOT NULL");
}
else {
    alert("This is NULL");
}
 

The output of the above code is “This is NULL.” This will never change because else block is always going to execute when you run code.

5: undefined

If you pass undefined to the expression, then it will always evaluate as false.

Example:

 
if(undefined) {
    alert("This is NOT undefined");
}
else {
    alert("This is undefined");
}
 


The output of the above code is “This is undefined.” This will never change because else block is always going to execute when you run code.

6. NaN (Not a Number)

If you pass NaN to the expression, then it will always evaluate as false.

Example:

 
if(NaN) {
    alert("This is NOT NaN");
}
else {
    alert("This is NaN");
}
 

Truthy Expressions in JavaScript

1. True

If you pass true inside if statement, then it will always be evaluated as true.

 
if(true) {
    alert("This Statement is True");
}
else {
    alert("This Statement is False");
}
 

The output of the above code is “This Statement is True.” This will never change because if block is always going to execute when you run code.

2. Number Other than 0

Any number other than 0, whether it is negative or positive, it evaluates as true.

Example:

 
if(0.1) {
    alert("This is 0.1");
}
else {
    alert("This is NOT 0");
}
 

The output of the above code is “This is 0.1“. The above number is not 0. So it will be evaluated as true always.

3. “0” (ZERO) as String

Number 0 (ZERO) evaluates as false, BUT string “0” will evaluate as true. When you put number 0 inside double quotes (“0”), it becomes a string.

Example:

 
if("0") {
    alert("This is String 0");
}
else {
    alert("This is NOT a String 0");
}
 

Output of above code is “This is String 0“. Value is string 0. So it will be evaluate as true always.

That’s it. This would be helpful for you when you are using conditional operators.

Categories js

Leave a Comment