The Difference Between == and === in JavaScript (Must Know)

One of the things that most often confuses beginner JavaScript developers — even experienced ones — is the difference between == and ===. Both are used to compare two values, but i...

The Difference Between == and === in JavaScript (Must Know)
Advertisement

One of the things that most often confuses beginner JavaScript developers — even experienced ones — is the difference between == and ===. Both are used to compare two values, but in very different ways. Understanding this difference matters because misusing them can produce bugs that are hard to track down.

== (Loose Equality)

The == operator compares two values after performing type conversion (type coercion). If the two sides have different data types, JavaScript will try to convert one of them so they can be compared:

// Comparing a number and a string
console.log(5 == '5');      // true — string '5' is converted to number 5
console.log(0 == '');       // true — empty string is converted to 0
console.log(0 == false);    // true — false is converted to 0
console.log(1 == true);     // true — true is converted to 1

// Comparing with null and undefined
console.log(null == undefined);  // true — this is a special case
console.log(null == 0);          // false — null only == undefined
console.log(null == false);      // false — null is not converted to false

// Comparing with strings and booleans
console.log('' == false);   // true — both are converted to 0
console.log('1' == true);   // true — '1' -> 1, true -> 1
console.log('2' == true);   // false — '2' -> 2, true -> 1, not equal

=== (Strict Equality)

The === operator compares values with no type conversion at all. If the data types differ, the result is immediately false:

Advertisement
// With ===, the data types MUST match
console.log(5 === '5');      // false — number vs string
console.log(0 === false);    // false — number vs boolean
console.log(1 === true);     // false — number vs boolean
console.log('' === false);   // false — string vs boolean
console.log(null === undefined); // false — different types

// === is only true if the value AND type match exactly
console.log(5 === 5);        // true
console.log('hello' === 'hello'); // true
console.log(true === true);  // true
console.log(null === null);  // true
console.log(undefined === undefined); // true

Why Is === More Recommended?

The type coercion behavior of == is often unintuitive and a source of confusing bugs. Consider these examples:

// Confusing cases with ==
console.log([] == false);    // true — weird!
console.log([] == ![]);      // true — very weird!
console.log('' == 0);        // true
console.log(' ' == 0);       // true — a space also equals 0!
console.log('0' == false);   // true
console.log(null == 0);      // false — even though null is considered "falsy"

// With === everything is clear and predictable
console.log([] === false);   // false — clearly not equal
console.log('' === 0);       // false — clearly not equal
console.log('0' === false);  // false — clearly not equal

Comparing null and undefined

One popular exception where == is actually useful: checking whether a value is null or undefined at the same time:

function process(value) {
  // Using == — catches both null AND undefined at once
  if (value == null) {
    console.log('Value is null or undefined, stop processing.');
    return;
  }
  console.log('Processing:', value);
}

process(null);      // 'Value is null or undefined...'
process(undefined); // 'Value is null or undefined...'
process(0);         // 'Processing: 0' — 0 is not included!
process('');        // 'Processing: ' — empty string is not included!

// With ===, you have to write the condition twice
if (value === null || value === undefined) {
  // same effect
}

Comparing Objects and Arrays

Both == and === compare objects and arrays by reference, not by content:

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

console.log(a == b);  // false — different references (different objects in memory)
console.log(a === b); // false — different references
console.log(a === c); // true — SAME reference (c points to the same object as a)
console.log(a == c);  // true — same reference

// To compare the CONTENTS of an array/object, use JSON.stringify (a simple way)
console.log(JSON.stringify(a) === JSON.stringify(b)); // true

!= vs !==

The same difference applies to the inequality operators:

// != uses type coercion
console.log(5 != '5');   // false — because 5 == '5' is true

// !== is strict, with no type conversion
console.log(5 !== '5');  // true — because the types differ (number vs string)

Simple Rules to Follow

  • Use === and !== as the default in all your JavaScript code.
  • Use == only to check for null or undefined at once (value == null), if that's what you actually want.
  • Linters like ESLint have an eqeqeq rule that warns you every time you use ==.

Conclusion

The difference between == and === comes down to whether JavaScript performs type conversion before comparing or not. The == operator performs type coercion, which often produces unexpected results, while === compares value and type strictly with no conversion. Make === your default habit so your code is more predictable, easier to read, and free of confusing bugs.

Advertisement
perbedaan == dan === javascript double equal triple equal js type coercion javascript strict equality javascript perbandingan nilai javascript
Share this article
Back to Blog
🚀 Partner Recommendation

Need Premium Source Code & Business Apps?

Access Laravel applications, POS systems, School Management, Clinic Software, ERP solutions, and ready-to-use premium source code at GudangCode.

GudangCode
  • ✔ Premium Source Code
  • ✔ Ready-to-Use Systems
  • ✔ Lifetime Updates
  • ✔ Lifetime Membership
  • ✔ Daily App Updates
Join Membership →
Advertisement
Advertisement