Explain The Concept of Truthy & Falsy Values in JavaScript
Last Updated :
09 Dec, 2024
In JavaScript, truthy and falsy values are concepts related to boolean evaluation. Every value in JavaScript has an inherent boolean "truthiness" or "falsiness," which means they can be implicitly evaluated to true or false in boolean contexts, such as in conditional statements or logical operations.
What Are Truthy Values?
Truthy values are values that are evaluated to be true when used in a Boolean context. Simply put, any value that is not explicitly falsy is considered truthy.
These are some truthy values
JavaScript
if (42) console.log("This is truthy!");
if ("hello") console.log("Non-empty strings are truthy!");
if ({}) console.log("Objects are truthy!");
OutputThis is truthy!
Non-empty strings are truthy!
Objects are truthy!
What Are Falsy Values?
Falsy values are values that evaluate to false when used in a Boolean. JavaScript has a fixed list of falsy values
- false
- 0 (and -0)
- 0n (BigInt zero)
- "" (empty string)
- null
- undefined
- NaN
- document.all (used for backward compatibility)
JavaScript
if (0) console.log("This won't run because 0 is falsy.");
if ("") console.log("This won't run because an empty string is falsy.");
if (null) console.log("This won't run because null is falsy.");
Truthy vs. Falsy Evaluation in JavaScript
Whenever JavaScript evaluates an expression in a Boolean (e.g., in an if statement, a logical operator, or a loop condition), it implicitly converts the value into true or false based on whether it is truthy or falsy.
With if Statement
JavaScript
let s = "JavaScript";
if (s) {
console.log("Truthy!");
} else {
console.log("Falsy!");
}
Logical Operators with Truthy and Falsy
Logical operators like && (AND) and || (OR) work with truthy and falsy values
- && (AND): Returns the first falsy operand or the last operand if all are truthy.
- || (OR): Returns the first truthy operand or the last operand if all are falsy.
JavaScript
console.log(true && "JavaScript");
console.log(false || "Hello!");
console.log(0 || null);
OutputJavaScript
Hello!
null
Explicit Boolean Conversion
You can explicitly check whether a value is truthy or falsy using the Boolean() function or the double negation operator (!!).
JavaScript
console.log(Boolean(42));
console.log(Boolean(0));
console.log(Boolean("hello"));
console.log(Boolean(""));
// Using !!
console.log(!!"world");
console.log(!!undefined);
Outputtrue
false
true
false
true
false
Common Pitfalls and Misunderstandings
Empty Strings vs. Non-Empty Strings
- "" (empty string) is falsy.
- " " (string with a space) is truthy.
JavaScript
if ("") console.log("Falsy"); // Won't run
if (" ") console.log("Truthy"); // Will run
Zero (0) vs. Non-Zero Numbers
0 is falsy, but -1, 3.14, and other numbers are truthy.
JavaScript
if (0) console.log("Falsy"); // Won't run
if (-1) console.log("Truthy"); // Will run
Empty Objects and Arrays Are Truthy
Unlike Python, where empty containers are falsy, empty objects {} and arrays [] are truthy in JavaScript.
JavaScript
if ([]) console.log("Empty arrays are truthy!");
if ({}) console.log("Empty objects are truthy!");
OutputEmpty arrays are truthy!
Empty objects are truthy!
Practical Applications
Default Values Using Logical OR (||)
The || operator is commonly used to assign default values when a variable is falsy.
JavaScript
let username = "";
let displayName = username || "Guest";
console.log(displayName);
Conditional Property Access
Truthy and falsy checks can be used to avoid errors when accessing object properties:
JavaScript
let user = null;
if (user && user.name) {
console.log(user.name); // Safely checks if user and user.name exist
}
Avoiding Explicit Comparisons
Truthy and falsy values allow concise conditions without explicit equality checks:
JavaScript
if (!value) {
console.log("Value is falsy.");
}
Difference Between Truthy and Falsy Values
Aspect | Truthy Values | Falsy Values |
---|
Definition | Values that evaluate to true in Boolean contexts. | Values that evaluate to false in Boolean contexts. |
Examples | 42, "hello", {}, [], function() {} | false, 0, -0, "", null, undefined, NaN |
Empty Structures | Empty objects {} and arrays [] are truthy. | Not applicable (empty structures are not falsy). |
Strings | Non-empty strings (e.g., "hello", " ") are truthy. | Empty strings ("") are falsy. |
Numbers | Non-zero numbers (e.g., 42, -3.14) are truthy. | Zero (0, -0) is falsy. |
BigInt | Any non-zero BigInt value (e.g., 10n) is truthy. | Zero BigInt (0n) is falsy. |
Usage in Logical Operators | Can short-circuit ` | |
Related Articles
Similar Reads
How to remove falsy values from an array in JavaScript ? Falsy/Falsey Values: In JavaScript, there are 7 falsy values, which are given below falsezero(0,-0)empty string("", ' ' , ` `)BigIntZero(0n,0x0n)nullundefinedNaNIn JavaScript, the array accepts all types of falsy values. Let's see some approaches on how we can remove falsy values from an array in Ja
6 min read
Explain non-boolean value coercion to a boolean one in JavaScript As we all know javascript is a forgiving language. It does not mind silly mistakes which programmers do. So, sometimes it gives unexpected results. Â So it senses Javascript says "I can do every possibility". Coercion is one of those in which javascript gives weird results because javascript automati
3 min read
Explain the purpose of the âinâ operator in JavaScript JavaScript in operator is used to check whether the data is within the object or in an array. In an object, the in operator works only on the key or property of the object. If the key or property exists then this operator returns true otherwise false. Similarly, for arrays, it will return true if we
2 min read
Explain the 'double negative' Trick in JavaScript In programming, a "double negative" is a technique used to convert a value to its corresponding boolean value. This technique involves using two negation operators (!) to negate a value twice, resulting in a boolean representation of the original value. The double negative technique is often used to
2 min read
Check if a Variable is of Function Type using JavaScript A function in JavaScript is a set of statements used to perform a specific task. A function can be either a named one or an anonymous one. The set of statements inside a function is executed when the function is invoked or called. javascriptlet gfg = function(){/* A set of statements */};Here, an an
3 min read
JavaScript - Why â0â Is Not Equal To false In if Condition? In JavaScript, the behaviour of 0 and 'false' conditional statements can be confusing. This is because JavaScript uses a process called type coercion, where it automatically converts values from one type to another to perform comparisons or evaluations. The reason behind this behaviour is that when
4 min read
When and why to 'return false' in JavaScript? Return statements, in programming languages, are used to skip the currently executing function and return to the caller function. Return statements may/may not return any value. Below is the example of a return statement in JavaScript. Example 1: javascript // The return statement returns // the pro
2 min read
Equality(==) Comparison Operator in JavaScript In JavaScript, the Comparison Equality Operator is used to compare the values of the operand. The comparison operator returns true only if the value of two operands are equal otherwise it returns false. It is also called a loose equality check as the operator performs a type conversion when comparin
2 min read
Strict Equality(===) Comparison Operator in JavaScript JavaScript Strict Equality Operator is used to compare two operands and return true if both the value and type of operands are the same. Since type conversion is not done, so even if the value stored in operands is the same but their type is different the operation will return false. Syntax: a===b E
2 min read
JavaScript â===â vs â==âComparison Operator In Javascript(ES6), there are four ways to test equality which are listed below: Using '==' operatorUsing '===' operatorSameValueZero: used mainly in sets, maps and arrays.SameValue: used elsewhereNow, our main concern is getting to know the difference between the '==' and '===' operators that the j
3 min read