# Equality of Values in JavaScript

Updated: April 19, 2023Understanding the equality of values helps prevent bugs. You often need to know when you are dealing with the same value or different values.

There are three kinds of equality in JavaScript:

**Same Value Equality**:`Object.is(a, b)`

**Strict Equality**:`a === b`

(triple equals)**Loose Equality**:`a == b`

(double equals)

## Same Value Equality

Same value equality tells us whether two values are the same value. You can use `Object.is(a, b)`

to compare any two values, primitive or object.

Let's look at an example using primitive values:

Let's look at an example using object values and remember that `{}`

creates a new object value. Also, remember that `=`

means "point the wire on the left side to the value on the right side".

## Strict Equality

Strict equality compares two values using the `===`

operator and there is also a corresponding opposite `!==`

operator.

In the previous examples, `a === b`

behaves in the same way as `Object.is(a, b)`

. However, there are two rare cases where the behavior of `===`

is different:

`NaN === NaN`

is`false`

, even though they are the same value`-0 === 0`

and`0 === -0`

are`true`

, even though they are different values.

### Strict Equality with `NaN`

`NaN`

is a special value in JavaScript that means "not a number" and appears when performing invalid calculations such as `0 / 0`

.

Remember that `NaN === NaN`

is **always** `false`

, even though `NaN`

is the same value as `NaN`

.

Also, if you want to check if a value is `NaN`

, instead of `value === NaN`

, here are some ways to check if the value is `NaN`

:

### Strict Equality with `-0`

and `0`

Both `0 === -0`

and `-0 === 0`

are always **true**, even though `0`

is a different value from `-0`

:

## Loose Equality

The rules of loose equality (or "abstract equality") are a bit confusing. In fact, it is uncommon in modern codebases, and many coding standards prohibit the use of `==`

and `!=`

in code.