A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/explain-the-concept-of-truthy-falsy-values-in-javascript/ below:

Explain The Concept of Truthy & Falsy Values in JavaScript

Explain The Concept of Truthy & Falsy Values in JavaScript

Last Updated : 05 Aug, 2025

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!");

Output
This 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

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

JavaScript
console.log(true && "JavaScript");
console.log(false || "Hello!");  
console.log(0 || null);  

Output
JavaScript
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);   

Output
true
false
true
false
true
false
Common Pitfalls and Misunderstandings Empty Strings vs. Non-Empty Strings 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!"); 

Output
Empty 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4