Open In App

Truthy In JavaScript

Last Updated : 09 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In JavaScript, truthy values are the values that are evaluated to be true when used in a Boolean context, such as in conditional statements or logical operations.

If you’ve ever wondered what JavaScript considers “true,” here’s the simple answer: any value that is not explicitly falsy (like false, 0, null, undefined, NaN, or an empty string "") is considered truthy. This means numbers (except 0), non-empty strings, objects, arrays, functions, and even some quirky values like "0" are all truthy.

List of Truthy Values in JavaScript

Here’s a detailed look at the most common truthy values in JavaScript:

Non-zero Numbers

Any number, whether positive or negative, is truthy.

JavaScript
if (42) console.log("This is truthy!");
if (-3.14) console.log("This is also truthy!");

Output
This is truthy!
This is also truthy!

Non-Empty Strings

Any string with at least one character is truthy, including strings containing whitespace.

JavaScript
if ("hello") console.log("Non-empty strings are truthy!"); 
if (" ") console.log("Even whitespace is truthy!");

Output
Non-empty strings are truthy!
Even whitespace is truthy!

Objects

All objects (including empty objects {}) are truthy.

JavaScript
if ({}) console.log("An empty object is truthy!");
if ({ key: "value" }) console.log("Objects with properties are truthy!"); 

Output
An empty object is truthy!
Objects with properties are truthy!

Arrays

Similarly, all arrays (including empty arrays []) are truthy.

JavaScript
if ([]) console.log("An empty array is truthy!"); 
if ([1, 2, 3]) console.log("Non-empty arrays are truthy!"); 

Output
An empty array is truthy!
Non-empty arrays are truthy!

Functions

Functions are always truthy, even if they do nothing.

JavaScript
if (function() {}) console.log("Functions are truthy!"); 

Output
Functions are truthy!

Dates

All JavaScript Date objects are truthy, regardless of their validity.

JavaScript
if (new Date()) console.log("Dates are truthy!");

Output
Dates are truthy!

Non-Zero BigInt Values

All BigInt values, except 0n, are truthy.

JavaScript
if (1n) console.log("BigInt is truthy!");

Output
BigInt is truthy!

Symbols

All Symbol values are truthy.

JavaScript
if (Symbol("id")) console.log("Symbols are truthy!"); 

Output
Symbols are truthy!

To better understand how truthy values work in JavaScript, Let's see an example

JavaScript
const a = [
    1,          // Truthy (non-zero number)
    -1,         // Truthy (negative number)
    "hello",    // Truthy (non-empty string)
    " ",        // Truthy (whitespace string)
    {},         // Truthy (empty object)
    [],         // Truthy (empty array)
    function () { }, // Truthy (function)
    new Date(), // Truthy (date object)
    Symbol(),   // Truthy (symbol)
    10n         // Truthy (BigInt)
];

a.forEach(value => {
    if (value) {
        // Handle Symbol conversion separately
        const displayValue = typeof value === "symbol" ? value.toString() : value;
        console.log(`${displayValue} is truthy.`);
    }
});

Output
1 is truthy.
-1 is truthy.
hello is truthy.
  is truthy.
[object Object] is truthy.
 is truthy.
function () { } is truthy.
Mon Dec 09 2024 08:00:36 GMT+0000 (Coordinated Universal Time) is truthy.
Sym...

Why Truthy Values Matter?

Understanding truthy values helps you write concise and readable code. For example:

Providing default values

JavaScript
let username
const user = username || "Guest";
console.log(user); // If username is falsy, prints "Guest"

Output
Guest

Checking for existence

JavaScript
if (config && config.apiKey) {
    console.log("API Key exists!");
}


Truthy values in JavaScript include any value that isn’t explicitly falsy. This simple rule makes JavaScript flexible but also requires attention to detail to avoid unexpected behavior. Remember, non-zero numbers, non-empty strings, objects, arrays, functions, symbols, and dates are all truthy—making JavaScript both powerful and occasionally quirky.

Related Articles


Next Article

Similar Reads