Truthy and Falsy Values in JavaScript
In JavaScript, every value behaves like true or false when used inside conditions.
Understanding which values act as true (truthy) and which act as false (falsy) is important because conditions, loops, and many logical checks depend on this behavior.
College students and freshers often get confused by unexpected results in conditions, so this chapter will make the concept very simple.
What Are Truthy and Falsy Values?
When JavaScript expects a boolean (true/false), it automatically converts other values into true or false.
Falsy values ? Act as false
Truthy values ? Act as true
This conversion is called type coercion in boolean context.
Falsy Values (Only 7 in JavaScript)
There are exactly 7 falsy values:
false0""(empty string)nullundefinedNaN-0
Everything else is truthy.
Example: Checking falsy values
if (0) {
console.log("True");
} else {
console.log("False");
}
Output:
False
Because 0 is a falsy value.
Truthy Values
Any value that is not falsy is truthy.
Examples:
"Hello"" "(space)"0"(string zero)5-5[ ](empty array){ }(empty object)true
Example: Checking truthy values
if ("Hello") {
console.log("This is truthy");
} else {
console.log("This is falsy");
}
Output:
This is truthy
Even an empty array {} or [] is truthy!
Why Is This Important?
Truthy and falsy values affect:
Conditions
Loops
Input validations
API responses
Form handling
Login checks
If you don’t understand them, you may get unexpected results.
Examples for Each Falsy Value
1. false
if (false) console.log("True"); else console.log("False");
Output:
False
2. 0
if (0) console.log("True"); else console.log("False");
Output:
False
3. "" (Empty string)
if ("") console.log("True"); else console.log("False");
Output:
False
4. null
if (null) console.log("True"); else console.log("False");
Output:
False
5. undefined
let a;
if (a) console.log("True"); else console.log("False");
Output:
False
6. NaN
if (NaN) console.log("True"); else console.log("False");
Output:
False
7. -0
if (-0) console.log("True"); else console.log("False");
Output:
False
Real-Life Example: Checking User Input
If the user enters nothing in a form:
let input = "";
if (input) {
console.log("Valid input");
} else {
console.log("Please enter something");
}
Output:
Please enter something
Because an empty string is falsy.
Real-Life Example: Checking a Login Value
let username = "rahul";
if (username) {
console.log("Login Successful");
} else {
console.log("Enter username");
}
Output:
Login Successful
Example Program With Multiple Truthy/Falsy Checks
let values = [0, 1, "", "Hello", null, undefined, [], {}];
for (let val of values) {
if (val) {
console.log(val, "? Truthy");
} else {
console.log(val, "? Falsy");
}
}
Output:
0 ? Falsy
1 ? Truthy
? Falsy
Hello ? Truthy
null ? Falsy
undefined ? Falsy
[] ? Truthy
{} ? Truthy
This clearly shows how different values behave.
Common Mistakes Beginners Make
Thinking
"0"is falsy (it is truthy).Thinking empty arrays are falsy (they are truthy).
Using
==instead of===(causes confusion with null/undefined).Not checking user input properly.
Forgetting that NaN is falsy.
Practice Tasks (Do It Yourself)
Check whether a string is empty or not.
Check if a number is provided; otherwise show an error.
Print truthy or falsy for:
0,"0",[],{},null,45,"".Create a login check where empty username should show an error.