===

The strict equality operator compares values without type conversion: true only for the same type and the same value.

Returns
true or false.
On this page

Example

#
JavaScript
console.log(1 === 1)
console.log(1 === '1')
console.log(1 == '1')
console.log(0 == '')
console.log(null == undefined)
console.log(null === undefined)
Console
true
false
true
true
true
false

Definition and usage

#

The === operator returns true when both sides have the same type and the same value. It never converts anything: the number 1 and the string "1" are different to it. Its negation is !==.

The loose operator == converts types before comparing, following convoluted rules, which is why 0 == "" and "1" == 1 are both true. That’s why modern code standardizes on ===. The one common exception is value == null, which checks for null and undefined at once.

Objects, arrays, and functions are compared by reference, not by content: two separately created objects are never equal to each other. A special case is NaN, which isn’t even equal to itself. Number.isNaN() and Object.is() are used to detect it.

Syntax

#
Syntax
a === b
a !== b

More examples

#
Objects are compared by reference
JavaScript
const a = { level: 1 }
const b = { level: 1 }
const c = a

console.log(a === b)
console.log(a === c)
console.log([1, 2] === [1, 2])
Console
false
true
false
NaN and Object.is()
JavaScript
const result = Number('abc')

console.log(result === NaN)
console.log(Number.isNaN(result))
console.log(Object.is(result, NaN))
Console
false
true
true
Form data comes in as strings
JavaScript
const input = '5'
const correctAnswer = 5

console.log(input === correctAnswer)
console.log(Number(input) === correctAnswer)
Console
false
true

Values from form fields and URL parameters are always strings. Convert them to a number explicitly before comparing, instead of relying on ==.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Always write === and !==. If you’re comparing values of different types, convert explicitly, e.g. with Number(value) or String(value).
  • To compare the contents of two arrays or objects, compare specific fields, or use a function for deep comparison. === only checks whether it’s the same object.

Related entries

#

Spotted a mistake or missing an example? Write to us.