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.
JavaScript == vs ===: learn the difference in plain English with simple, runnable examples, and find out why === is the safer choice for beginners.
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.
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 returns true only when both the value and the type match. No conversion happens.
console.log(5 === 5); // true
console.log(5 === "5"); // false
console.log("hi" === "hi"); // true
console.log(true === 1); // falseLine 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 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.
console.log(5 == "5"); // true
console.log(true == 1); // true
console.log(0 == ""); // true
console.log(0 == "0"); // true
console.log("" == "0"); // falseLine 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 ==.
| Expression | == result | === result |
|---|---|---|
| 5 and "5" | true | false |
| 0 and false | true | false |
| null and undefined | true | false |
| "a" and "a" | true | true |
| NaN and NaN | false | false |
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).
Values typed into a web form always arrive as strings. This is a common place where beginners get stuck.
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.
Each equality operator has a "not equal" partner. != is the loose version and !== is the strict version. The same advice applies: prefer !==.
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.
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.
const a = [1, 2];
const b = [1, 2];
const c = a;
console.log(a === b); // false
console.log(a === c); // truea 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 constThere 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.
== 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.
Use === in almost all cases. It avoids surprising type conversion and prevents hard-to-find bugs. Most style guides and linters recommend it.
By the rules of JavaScript, NaN (not a number) is never equal to anything, including itself. Use Number.isNaN(value) to test for it.
Yes. With ==, false is converted to 0, so the result is true. With ===, the types differ (number and boolean), so the result is false.
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 FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.