Skip to main content

How to Check if a Function Returns true or a Truthy Value in JavaScript

When working with functions that return a boolean or a value that implies a boolean state, you often need to check the result in a conditional statement. However, there is a critical and subtle distinction in JavaScript between checking if a value is strictly true and checking if it is "truthy."

This guide will clarify this important difference and teach you the correct way to perform both types of checks on a function's return value.

The Core Concept: true vs. "Truthy"

Understanding this distinction is fundamental to JavaScript.

  • Strict true: This is the boolean primitive true. A check for strict true will only pass if the value is exactly true and not any other value.
  • "Truthy": In a boolean context (like an if statement), JavaScript coerces values into booleans. A value is considered "truthy" if it is not one of the few "falsy" values.
    • Falsy Values: false, 0, "" (empty string), null, undefined, NaN.
    • Truthy Values: Everything else. This includes non-empty strings ("hello"), any number other than zero (1, -1), arrays ([]), and objects ({}).

Scenario 1: Checking for a Strict true Value

You should perform this check when you need to be absolutely certain that a function returned the specific boolean value true.

For example, we have a function and we only want to proceed if it explicitly returns true.

// This function returns a truthy string, not the boolean `true`.
function isReady() {
return "Ready";
}

The recommended solution is to use the Strict Equality Operator (===). To check for strict equality, you must use the === operator.

function returnsTrue() {
return true;
}

function returnsTruthyString() {
return 'true';
}

// --- Check 1: The function returns the boolean `true` ---
if (returnsTrue() === true) {
// This block will run
console.log('✅ The function returned the boolean `true`.');
} else {
console.log('⛔️ The function did not return the boolean `true`.');
}

// --- Check 2: The function returns a truthy string ---
if (returnsTruthyString() === true) {
console.log('✅ The function returned the boolean `true`.');
} else {
// This block will run
console.log('⛔️ The function did not return the boolean `true`.');
}

Output:

✅ The function returned the boolean `true`.
⛔️ The function did not return the boolean `true`.

Scenario 2: Checking for a Truthy Value (More Common)

This is a far more common scenario. You want to check if a function returned a "successful" or "existing" value. For example, a function that finds a user might return a user object (truthy) on success or null (falsy) on failure.

For example, we have a function that returns either a value (truthy) or nothing (falsy), and we want to run code if a value was returned.

function findUser(id) {
if (id === 1) {
return { name: 'Alice' }; // A truthy object
}
return null; // A falsy value
}

The recommended solution is to use the if Statement. The simplest and most readable way to check for a truthy value is to use the function's return value directly in an if statement.

function findUser(id) {
if (id === 1) {
return { name: 'Alice' }; // truthy
}
return null; // falsy
}

// --- Check 1: A user is found ---
if (findUser(1)) {
// This block will run because an object is truthy
console.log('✅ A user object was returned (truthy).');
} else {
console.log('⛔️ A falsy value was returned.');
}

// --- Check 2: A user is NOT found ---
if (findUser(2)) {
console.log('✅ A user object was returned (truthy).');
} else {
// This block will run because `null` is falsy
console.log('⛔️ A falsy value was returned.');
}

Output:

✅ A user object was returned (truthy).
⛔️ A falsy value was returned.

Conclusion: Which Check Should You Use?

The choice is simple and depends on the expected behavior of your function.

  • Use a strict equality check (myFunction() === true) when you need to confirm that a function explicitly returned the boolean primitive true. This is a specific and less common requirement.
  • Use a truthy check (if (myFunction())) when you want to confirm that a function returned any valid data and not a falsy value (like null, undefined, false, 0, or ""). This is the most common and idiomatic way to handle conditional logic in JavaScript.