Understanding Truthy and Falsy Values in JavaScript
JavaScript associates every value with a Boolean state: true (truthy) or false (falsy). These concepts are foundational in JavaScript and critical when writing conditional statements.
What are Truthy and Falsy Values?
Truthy
In JavaScript, any value that evaluates to true in a Boolean context is considered truthy. Most values fall into this category.
Falsy
On the other hand, falsy values are those that evaluate to false in a Boolean context. There are only a handful of these values:
false
0
-0
""
(empty string)null
undefined
NaN
(Not a Number)
Why Do Truthy and Falsy Values Matter?
When writing conditions in JavaScript, the language uses these truthy and falsy values to determine the flow of code. Understanding these can help you write concise and bug-free programs.
Examples and Code Snippets
Example 1: Checking Boolean Literals
if (true) {
console.log("It has a truthy value");
} else {
console.log("It has a falsy value");
}
Output:It has a truthy value
Similarly, false
is inherently a falsy value:
if (false) {
console.log("It has a truthy value");
} else {
console.log("It has a falsy value");
}
Output:It has a falsy value
Example 2: Numbers
Non-Zero Numbers
All non-zero numbers are truthy, regardless of being positive or negative:
if (1) {
console.log("It has a truthy value");
} else {
console.log("It has a falsy value");
}
Output:It has a truthy value
Zero
The number 0
is falsy:
if (0) {
console.log("It has a truthy value");
} else {
console.log("It has a falsy value");
}
Output:It has a falsy value
Example 3: Strings
Non-Empty Strings
Any non-empty string, even a single space " "
, is truthy:
let string = "hello";
if (string) {
console.log("String is not empty");
} else {
console.log("String is empty");
}
Output:String is not empty
Empty Strings
An empty string (""
) is falsy:
let string = "";
if (string) {
console.log("String is not empty");
} else {
console.log("String is empty");
}
Output:String is empty
Example 4: Null and Undefined
Both null
and undefined
are falsy values in JavaScript:
if (null) {
console.log("It has a truthy value");
} else {
console.log("It has a falsy value");
}
Output:It has a falsy value
Example 5: Logical Conditions
JavaScript uses truthy and falsy values extensively in logical conditions. Here's an example with numbers:
let num = 0;
if (num) {
console.log("Number is not zero");
} else {
console.log("Number is zero");
}
Output:Number is zero
Common Pitfalls to Avoid
Misinterpreting Falsy Values:
Ensure you understand that even0
,""
, ornull
are falsy. These can lead to bugs if overlooked in conditions.Using Truthy/Falsy Without Explicit Comparison:
Be cautious when relying on implicit Boolean conversion. For clarity, you can use explicit comparison:if (string !== "") { console.log("String is not empty"); }
Practice Problems
Problem 1: Check If a String is Empty
Write a program to check if a given string is empty:
let string = "";
if (string) {
console.log("String is not empty");
} else {
console.log("String is empty");
}
Problem 2: Check If a Number is Zero
Write a program to check if a number is zero:
let num = 0;
if (num) {
console.log("Number is not zero");
} else {
console.log("Number is zero");
}