Skip to content

== vs === equal operators in JavaScript, what's the difference?

`==` and `===`, two different operators to check for object equality. Which one to choose?

In JavaScript you can use two different operators to check for object equality. They are == and ===.

They basically do the same thing, but there is a big difference between the two.

=== will check for equality of two values. If they are objects, the objects must be of the same type. JavaScript is not typed, as you know, but you have some fundamental types which you must know about.

In particular we have value types (Boolean, null, undefined, String and Number) and reference types (Array, Object, Function).

If two values are not of the same type, === will return false.

If they are of the same type, JavaScript will check for equality.

With reference types, this means the values need to reference the same object / array / function. Not one with the same values: the same one.

== is different because it will attempt to convert types to match.

This is why you get results like

false == '0'  //true
false === '0' //false
null == undefined //true
null === undefined  //false

In my experience, in 97% of the cases you’ll want to use ===, unless == provides exactly what you want. It has less drawbacks and edge cases.

The same goes for != and !==, which perform the same thing, but negated.

Always default to !==.

→ Get my JavaScript Beginner's Handbook

I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter

  • C Handbook
  • Command Line Handbook
  • CSS Handbook
  • Express Handbook
  • Git Cheat Sheet
  • Go Handbook
  • HTML Handbook
  • JS Handbook
  • Laravel Handbook
  • Next.js Handbook
  • Node.js Handbook
  • PHP Handbook
  • Python Handbook
  • React Handbook
  • SQL Handbook
  • Svelte Handbook
  • Swift Handbook

JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list