HuntExams Academy logo
JavaScriptBeginner#coercion

What are truthy and falsy values?

Answer

Falsy values are false, 0, -0, 0n, '', null, undefined and NaN. Everything else is truthy, including '0', 'false', [] and {}.

Example

if ([]) console.log('empty array is truthy'); // prints
Boolean('0');   // true
Boolean(NaN);   // false

Detailed explanation

In JavaScript, truthy and falsy describe how a value behaves when JavaScript needs a Boolean decision. A truthy value is treated as true, while a falsy value is treated as false. This does not mean the value itself becomes a Boolean or changes type. These rules matter in if statements, loops, ternary expressions, and logical operators because conditions often contain strings, numbers, or objects rather than explicit true or false values.

The falsy values you should know are false, 0, -0, 0n, the empty string '', null, undefined, and NaN. Different quote styles make no difference: "" and an empty template literal are also empty strings. All other ordinary JavaScript values are truthy, including nonzero numbers, negative numbers, '0', 'false', whitespace-only strings, empty arrays [], and empty objects {}. There is one historical browser exception: document.all is an unusual legacy object that is falsy. For beginner interviews, explain the standard list first and mention this exception only if completeness is relevant.

Under the hood, JavaScript applies its ToBoolean conversion rules when evaluating a condition. For example, if ([]) console.log('empty array is truthy') prints because an array is an object, and ordinary objects are truthy regardless of their contents. Boolean('0') returns true because the string is not empty, whereas Boolean(NaN) returns false. You can also use double negation, such as !!value, to obtain a Boolean. This conversion does not call an object's valueOf() or toString() methods, so even new Boolean(false) is truthy: it is a wrapper object.

Interviewers often check whether you can distinguish truthiness from equality and choose safe defaults. A truthy value is not necessarily loosely equal to true; [] is truthy even though [] == false evaluates to true through different coercion rules. Also, || and && return operands, not necessarily Booleans. Using count || 10 replaces a valid zero with 10; count ?? 10 preserves zero and defaults only for null or undefined.

Key points to remember

  • Truthy and falsy describe Boolean-context behaviour, not a value's original type.
  • Standard falsy values are false, 0, -0, 0n, '', null, undefined, and NaN.
  • Nonempty strings, empty arrays, and empty objects are truthy; document.all is a legacy browser exception.
  • Boolean(value) and !!value explicitly produce a Boolean.
  • || defaults on any falsy value; ?? defaults only on null or undefined.

Common mistakes candidates make

  • Assuming empty arrays, empty objects, '0', or 'false' are falsy.
  • Using value == false as a truthiness test instead of !value.
  • Using || for defaults when 0, false, or an empty string should remain valid.

Likely follow-up questions

Why is an empty array truthy but loosely equal to false?

Boolean conversion treats an ordinary array object as truthy without inspecting its contents. Loose equality uses different coercion rules: [] converts to an empty string and then zero, while false converts to zero.

What do && and || return?

&& returns the first falsy operand, or the last operand if all are truthy; || returns the first truthy operand, or the last operand if all are falsy. They short-circuit, so later operands are evaluated only when needed.

How should you check whether an array is empty?

For a known array, check arr.length === 0 rather than testing !arr. If the value might not be an array, first validate it with Array.isArray(arr).

Interview tip: Define Boolean-context behaviour, list the falsy values, and demonstrate one surprising truthy value before explaining a practical defaulting pitfall.

Related Interview Questions

1
JavaScriptIntermediate#this#functions

How does 'this' work in JavaScript?

Open
2
JavaScriptIntermediate#functions#this

Difference between call, apply and bind?

Open
3
JavaScriptIntermediate#functions#es6

Difference between arrow functions and regular functions?

Open