Skip to content
BasicsChapter 12 / 47Beginner5 min read

Truthy, Falsy & Equality

The list of JavaScript truthy and falsy values, the difference between == and ===, Object.is and how objects are compared by reference.

On this page

1. What are truthy, falsy and equality?

In JavaScript, only these 8 values are falsy. Everything else is truthy:

Falsy valueType
falseboolean
0, -0number
0nbigint
""empty string
nullnull
undefinedundefined
NaNnumber

Surprising truthy values: "0", "false", " " (a space), [], {}.

2. Why does it matter?

if (value) is written everywhere. If you do not know that 0 is falsy, an item with quantity 0 wrongly becomes "missing". And the strange rules of == cause bugs that are hard to catch.

Loose equality ==Strict equality ===
Converts types first, then comparesIf the types differ, it is simply false
0 == empty string gives true0 === empty string is false
You have to remember the rulesThe result is what you see
Hides bugsPredictable and safe

3. Where is it used?

SituationThe right way
Input is not emptyif (name.trim())
The array has itemsif (items.length > 0), because [] is truthy
Value is null or undefinedvalue == null or value ?? fallback
Matching two values===
NaN checkNumber.isNaN(x) or Object.is(x, NaN)
Are two objects the same?=== only checks the reference

4. How does it work?

A condition (if, while, &&, ||, !) converts the value to a boolean internally. It works like Boolean(value).

A value's journey in a condition
if (value)
Boolean(value) runs internally
Is it in the falsy list? Then false
Otherwise true

=== looks at the type first. If the types differ, it is false right away. == applies coercion rules when the types differ, like turning a string into a number.

For objects and arrays, both operators compare the reference, not the content.

5. Code example

Truthy and falsy:

js
const values = [0, "", "0", [], {}, null, NaN, " "];

for (const v of values) {
  console.log(JSON.stringify(v), Boolean(v));
}

Output: 0 false, "" false, "0" true, [] true, {} true, null false, null false (NaN shows as null in JSON), " " true.

== vs ===:

js
console.log(0 == "");
console.log(0 === "");
console.log("1" == 1);
console.log(null == undefined);
console.log(null === undefined);
console.log(null == 0);
console.log(NaN === NaN);

Output: true, false, true, true, false, false, false.

Objects and Object.is:

js
const a = { id: 1 };
const b = { id: 1 };
const c = a;

console.log(a === b);
console.log(a === c);
console.log(Object.is(NaN, NaN));
console.log(Object.is(0, -0));

Output: false, true, true, false.

How does the code work?

ItemMeaning
Boolean(v)The same conversion that if does internally
0 == """" became the number 0, so true
null == undefinedA special rule, they are equal only to each other
NaN === NaNNaN is not equal to anything, not even itself
a === bSame content, but different objects, so false
Object.is()Like ===, but handles NaN and -0 correctly

Interview tip

The answer to [] == false is true, yet if ([]) runs. The reason: == turns both into numbers ([] to "" to 0, and false to 0), while if only looks at truthiness, and [] is truthy. This question is a great argument for avoiding ==.

6. Common mistakes

Mistake: Using if (count) to check that a number exists

Why? If count is 0 it becomes falsy, and a valid 0 is treated as missing.

Better: Write a direct check, like count !== undefined or typeof count === 'number'.

Mistake: Checking an empty array with if (arr)

Why? An empty array is also truthy, so the condition always passes.

Better: Check arr.length > 0.

Mistake: Using ==

Why? The coercion rules give strange results, like '0' == false being true.

Better: Always use === and !==. If you need to check null and undefined together, value == null is fine.

Mistake: Comparing two objects with === to check their content

Why? === only checks whether both are the same object, not the data inside.

Better: Compare specific fields, like a.id === b.id.

7. What to learn before and after

Final quick revision

PointDetail
Falsyfalse, 0, -0, 0n, "", null, undefined, NaN
Truthy surprises"0", "false", [], {}
===Same type and value, the default choice
==Does coercion, avoid it
ObjectsThe reference is compared, not the content
Object.isExact for NaN and -0