What are truthy and falsy values in JavaScript?
Advertisement
🧩 Scenario
Architecture Walkthrough
The Complete Falsy List
JavaScript has exactly seven falsy values: false, 0, -0, 0n (BigInt zero), "" (empty string), null, undefined, and NaN. Every other value is truthy. This includes empty arrays ([]), empty objects ({}), the string "false", and the number Infinity.
The list is short enough to memorize, and it is worth memorizing precisely. The values that surprise developers most often are 0n (which many do not know about) and empty collections. An empty array or object is truthy because objects in JavaScript are always truthy, regardless of whether they contain anything.
Short-Circuit Evaluation
The && and || operators in JavaScript do not return true or false. They return one of their operands. && returns the first falsy value it encounters, or the last value if all are truthy. || returns the first truthy value it encounters, or the last value if all are falsy.
This behavior powers short-circuit evaluation patterns. user && user.name is a safe way to access a property only if the object exists. user.role || 'viewer' provides a default role. These patterns are common and readable, but they carry a risk: if the left side of || is a valid falsy value like 0 or an empty string, the default will be applied even when you do not want it to be.
Nullish Coalescing for Safer Defaults
The nullish coalescing operator (??) was introduced to solve the || false-positive problem. While || triggers on any falsy value, ?? only triggers on null and undefined. This means count ?? 0 will return the value of count even if count is 0, whereas count || 0 would replace a valid 0 with 0 accidentally.
Use || when you want any falsy value to trigger the default. Use ?? when you only want to fall back on null or undefined. This distinction matters most for numeric and string values that could legitimately be empty or zero.
Key Code Explained
// Falsy values
Boolean(false); // false
Boolean(0); // false
Boolean(-0); // false
Boolean(0n); // false
Boolean(''); // false
Boolean(null); // false
Boolean(undefined); // false
Boolean(NaN); // false
// Surprising truthy values
Boolean([]); // true — empty array is truthy
Boolean({}); // true — empty object is truthy
Boolean('false'); // true — non-empty string is truthy
Boolean(-1); // true — all non-zero numbers are truthy
// || vs ?? for defaults
const count = 0;
console.log(count || 10); // 10 — wrong if 0 is valid
console.log(count ?? 10); // 0 — correct: only null/undefined triggers ??
// Short-circuit in practice
const user = null;
const name = user && user.name; // null — short-circuits at user
const role = user?.role ?? 'viewer'; // 'viewer' — optional chain + nullish coalesce
The count || 10 vs count ?? 10 comparison is the most important practical takeaway. It appears in real bugs where user-entered 0 for a quantity field is treated as missing data.
Tradeoffs
| Pattern | Pro | Con |
|---|---|---|
value || default | Concise, familiar | Replaces any falsy value, not just null/undefined |
value ?? default | Only triggers on null/undefined, safer for 0 and '' | Not available in older environments without a polyfill |
Explicit === null || === undefined | Precise, readable | More verbose, easy to forget the undefined check |
What Interviewers Actually Check
- Whether you can list all falsy values including
0nand-0 - Whether you know empty arrays and objects are truthy and can explain why
- Whether you understand the difference between
||and??for defaults - Whether you can identify a bug caused by a valid
0being treated as falsy - Whether you can explain short-circuit evaluation for both
&&and||
Follow-Up Questions
- What does
!!valuedo, and when would you use it overBoolean(value)? - How does
&&behave differently from||in a short-circuit expression? - If a function returns
0on success andnullon failure, how would you check for failure without accidentally treating0as a failure? - What is
document.alland why is it the only truthy object that is also considered falsy? - Optional chaining (
?.) and nullish coalescing (??) were introduced together. How do they complement each other?
Common Candidate Mistakes
- Thinking
[]or{}is falsy because it is empty, when all objects are truthy - Not knowing that
0n(BigInt zero) is falsy - Using
||to provide a fallback for a numeric or string value without realizing it replaces valid0and"" - Checking
if (users)to guard an array and not realizing an empty array passes the check - Not knowing
??exists and manually writingvalue === null || value === undefinedevery time
Interview Readiness Checklist
Before you leave this question, make sure you can answer:
- Can you list all 7 falsy values from memory, including
0nand-0? - Can you explain why
[]and{}are truthy even though they are empty? - Can you explain the difference between
||and??for providing default values? - Can you identify a bug where a valid falsy value like
0is treated as missing by a||check? - Can you explain short-circuit evaluation for
&&and||?
Summary
JavaScript coerces every value to a boolean in conditional contexts. Only seven values are falsy: false, 0, -0, 0n, "", null, undefined, and NaN. Everything else, including empty arrays and empty objects, is truthy.
The most important practical application of this knowledge is in default value patterns. The || operator returns the first truthy value, which can produce incorrect results when the left side is a valid falsy value like 0 or "". The ?? operator was introduced to address this: it only falls through to the right side when the left side is specifically null or undefined.
Knowing the complete falsy list and the difference between || and ?? covers the two most common sources of bugs related to truthiness in modern JavaScript.
Is an empty array [] truthy or falsy?
It is truthy, even though it is empty. All objects (including arrays) are truthy in JavaScript.
Advertisement
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.
Advertisement