All posts
JavaScriptNovember 17, 20266 min read

JavaScript == vs === Explained for Beginners (Simple)

JavaScript == vs ===: learn the difference in plain English with simple, runnable examples, and find out why === is the safer choice for beginners.

F
Fepiq Team
Fepiq

What is the difference between == and === in JavaScript? In short: === (strict equality) checks that two values have the same value AND the same type, while == (loose equality) first tries to convert the values to the same type and then compares them. For beginners, the safe rule is simple: use === almost every time.

In this guide you will learn what each operator does, see small examples you can paste into your browser console, and find out why == can give surprising results. No experience is needed.

What does equality mean in JavaScript?

An operator is a symbol that does something with values, like + or =. An equality operator compares two values and gives back a boolean. A boolean is a value that is either true or false. JavaScript has two main ways to check if values are equal: == and ===.

A type is the kind of a value. For example, 5 is a number and "5" (with quotes) is a string, which means text. They look alike, but JavaScript treats them as different types. If you want a refresher, read our guide on variables and data types.

Strict equality: ===

Strict equality returns true only when both the value and the type match. No conversion happens.

js
console.log(5 === 5);      // true
console.log(5 === "5");    // false
console.log("hi" === "hi"); // true
console.log(true === 1);   // false

Line 1: both are the number 5, so the result is true. Line 2: one is a number and one is a string, so the result is false. Line 3: two identical strings are equal. Line 4: true is a boolean and 1 is a number, so they are not strictly equal. Each line is easy to predict, and that is the whole point.

Loose equality: ==

Loose equality tries to convert (change) one or both values to a common type before comparing. This is called type coercion. It can feel helpful, but it often hides bugs.

js
console.log(5 == "5");    // true
console.log(true == 1);   // true
console.log(0 == "");     // true
console.log(0 == "0");    // true
console.log("" == "0");   // false

Line 1: the string "5" is turned into the number 5, so it is true. Line 2: true becomes 1. Line 3: an empty string becomes 0. Line 4: "0" becomes 0. Line 5: both are strings, so no conversion happens, and "" is not the same text as "0". Notice that 0 equals both "" and "0", yet those two are not equal to each other. That kind of confusing result is why most developers avoid ==.

Side-by-side comparison

Expression== result=== result
5 and "5"truefalse
0 and falsetruefalse
null and undefinedtruefalse
"a" and "a"truetrue
NaN and NaNfalsefalse

Whenever the types differ, === says false right away. The last row is a special case: NaN means "not a number", and it is never equal to anything, even itself. To check for it, use Number.isNaN(value).

A real example: a number from a form

Values typed into a web form always arrive as strings. This is a common place where beginners get stuck.

js
const input = "18"; // from a text box

if (input === 18) {
  console.log("Adult");
} else {
  console.log("Not matched");
}

if (Number(input) === 18) {
  console.log("Adult");
}

The first if prints "Not matched", because "18" is a string and 18 is a number. This looks like a bug, but === is actually helping you notice the mismatch. The fix is in the second if: Number(input) converts the string to a number on purpose, and then === works. You decide when conversion happens, instead of JavaScript guessing.

Not equal: != and !==

Each equality operator has a "not equal" partner. != is the loose version and !== is the strict version. The same advice applies: prefer !==.

js
console.log(5 !== "5"); // true (different types)
console.log(5 != "5");  // false (converted, so equal)

The first line is true because a number and a string are not strictly the same. The second is false because != converts "5" to 5 before comparing.

Comparing objects and arrays

Both operators compare objects and arrays by reference, not by content. A reference is like the address of where the value lives in memory. Two separate arrays are never equal, even if they look the same.

js
const a = [1, 2];
const b = [1, 2];
const c = a;

console.log(a === b); // false
console.log(a === c); // true

a and b are two different arrays that happen to hold the same items, so the result is false. c points to the very same array as a, so the result is true. Learn more in our objects guide when you are ready.

Want to understand how let, const and var work with values like these? Read our beginner guide next.

Read var vs let vs const

When is == okay?

There is one common, accepted use: value == null checks for both null and undefined at once. Still, as a beginner, you can skip this trick and write value === null || value === undefined. It is longer but clearer.

Quick rules to remember

  • Use === and !== by default.
  • Convert types yourself with Number() or String() when needed.
  • Do not rely on == to guess what you mean.
  • Remember that arrays and objects compare by reference.
  • Use Number.isNaN() to check for NaN.

Frequently asked questions

What is the difference between == and === in JavaScript?+

== compares values after converting them to the same type, so 5 == "5" is true. === compares value and type with no conversion, so 5 === "5" is false. Using === makes your code more predictable.

Should I use == or === in JavaScript?+

Use === in almost all cases. It avoids surprising type conversion and prevents hard-to-find bugs. Most style guides and linters recommend it.

Why is NaN === NaN false?+

By the rules of JavaScript, NaN (not a number) is never equal to anything, including itself. Use Number.isNaN(value) to test for it.

Is 0 == false true in JavaScript?+

Yes. With ==, false is converted to 0, so the result is true. With ===, the types differ (number and boolean), so the result is false.

Does === work for comparing two arrays?+

Not by content. Two arrays are strictly equal only if they are the exact same array in memory. Two separate arrays with the same items give false.

Learning to code and want a website or app built by experts? Talk to the Fepiq team.

Contact Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.