Skip to content
Laviyx Learn

Try

JavaScript course menu

JavaScript Comparisons: === vs ==, Truthy and Falsy Values

Understand JavaScript comparisons: strict equality (===) versus loose equality (==), comparing strings, numbers and objects, NaN and Object.is, the complete list of falsy values, truthy values, and safe ways to check for empty or missing values.

Beginner8 min readUpdated
On this page

Conditions are built from comparisons, so it is important to know exactly how JavaScript compares values. This lesson covers the two kinds of equality and the idea of truthy and falsy values.

=== versus ==#

Strict equality === is true only when the values have the same type and the same value. Loose equality == converts the types first, using complicated rules:

JavaScript
console.log(5 === 5);
console.log(5 === "5");
console.log(5 == "5");
console.log(0 == "");
console.log(0 == "0");
console.log("" == "0");
console.log(null == undefined);
console.log(null === undefined);
Output
true
false
true
true
true
false
true
false

0 == "" and 0 == "0" are both true, but "" == "0" is false: loose equality is not even consistent with itself. Always use === and !==. The one common exception is value == null, a deliberate shortcut that is true for both null and undefined.

Comparing numbers and strings#

<, >, <= and >= compare numbers numerically and strings alphabetically, character by character (by Unicode code):

JavaScript
console.log(10 > 9);
console.log("10" > "9");
console.log("apple" < "banana");
console.log("Zebra" < "apple");
Output
true
false
true
true

"10" > "9" is false because, as text, "1" comes before "9". Uppercase letters come before lowercase ones in Unicode, so "Zebra" < "apple". Convert to numbers or use localeCompare (Strings) when the order matters.

Comparing objects#

Objects and arrays are compared by reference: two different objects are never equal, even with the same contents:

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

console.log(a === b);
console.log(a === c);
console.log([1, 2] === [1, 2]);
Output
false
true
false

To compare contents, compare the properties you care about (a.id === b.id), or for simple data, compare JSON.stringify(a) === JSON.stringify(b) (which depends on property order).

NaN and Object.is#

JavaScript
console.log(NaN === NaN);
console.log(Number.isNaN(NaN));
console.log(Object.is(NaN, NaN));
console.log(Object.is(0, -0), 0 === -0);
Output
false
true
true
false true

Object.is is like === but treats NaN as equal to itself and distinguishes 0 from -0. You rarely need it; Number.isNaN is the usual check.

Truthy and falsy#

When a value is used as a condition (in if, while, &&, || or !), JavaScript converts it to a boolean. There are exactly eight falsy values:

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

Everything else is truthy, including "0", "false", " " (a space), [] (an empty array), {} (an empty object) and every function.

JavaScript
const values = [0, "0", "", " ", [], {}, null, "false"];
for (const v of values) {
  console.log(JSON.stringify(v), v ? "truthy" : "falsy");
}
Output
0 falsy
"0" truthy
"" falsy
" " truthy
[] truthy
{} truthy
null falsy
"false" truthy

Checking for empty values safely#

Truthiness is convenient, but it can treat valid values like 0 as missing:

JavaScript
const quantity = 0;

if (!quantity) {
  console.log("Using truthiness: quantity looks missing");
}
if (quantity === undefined || quantity === null) {
  console.log("Never printed: 0 is a real value");
} else {
  console.log("Explicit check: quantity is", quantity);
}
Output
Using truthiness: quantity looks missing
Explicit check: quantity is 0
To checkWrite
A string has texttext.trim() !== "" or text.trim().length > 0
An array has itemslist.length > 0 (an empty array is truthy!)
An object has keysObject.keys(obj).length > 0
A value is missingvalue == null or value === undefined
A number was given (0 allowed)typeof n === "number" && !Number.isNaN(n)

Practice#

Practice exercise

Write a loop that prints, for each of these values, whether it is "missing" (only null or undefined) or "present": 0, "", null, false, undefined, "hello".

Show solution
JavaScript
const values = [0, "", null, false, undefined, "hello"];
for (const v of values) {
  console.log(String(v).padEnd(10), v == null ? "missing" : "present");
}
Output
0          present
           present
null       missing
false      present
undefined  missing
hello      present

v == null is true only for null and undefined, so 0, "" and false count as present. (The empty string prints as blank space.)

Key takeaway#

Finished this lesson?