Equality Operator in Javascript

Navita Singhal
The Startup
Published in
4 min readJun 28, 2020

What are Equality operators? Does ‘==’ and ‘===’ mean same and serve the same purpose? Do we really need two operators for checking the equality of a statement?

Equality in javascript

Before getting into equality operators its important that you understand data types in javascript. Javascript is a loosely typed and dynamic language. The variable is dynamically associated with a type based on the value. A given variable can be assigned and re-assigned different types.
There are Primitive and Complex data types.
Primitive Data types: Number, String, Boolean, undefined, null, BigInt, and Symbol.
Complex Data types: object and function

Type coercion(typecasting) is the process of converting a value from one type to another. Type coercion can be implicit or explicit.
Implicit type coercion is when a value is converted between different types automatically.
For example: 2/’5' // results in 0.4
Explicit type coercion is when the developer explicitly converts a value from one type to another.
For example: String(765) // results in “765”
Equality operators are part of javascript comparison operators they evaluate the statement to either true or false.

JavaScript has 2 operators for checking equality ‘==’ and ‘===’. We are here to understand what is the difference between these two operators??

Loose Equality operator (‘==’)

Compare values to check if they are equal and implicit type coercion. In simple terms, if you try to compare values of different types it will take the trouble of typecasting and check if the values are equal. We need to understand how the type coercion works internally in javascript. Here are 3 rules which will help you understand the ‘==’ operator better.

  1. Comparing a string and a number
    Javascript converts the string into a number if possible. An empty string is evaluated as 0.
    99 == “99” //evaluates to true (“99” gets converted to 99)
    66 == “hello” //evaluates to false (“hello” gets converted to NaN)
    “” == 0 //true
    When we try to convert a string into a number (“hello”) that can not be converted into a number we get NaN.
  2. Comparing a boolean with any other type
    In this case, the boolean gets converted into a number true is evaluated as 1 and false as 0.
    false == 0 // true (false gets converted to 0)
    false == “0” // true (false gets converted to 0 and according to first rule “0” evaluates to 0)
    “false” == “0” //false
  3. Comparing null and undefined
    In this case, javascript treats them as equal because essentially both mean “no value” (It doesn't make sense but that's what it is).
    null == undefined //true

That looks like a lot of rules to be kept in mind while checking equality between two values. That’s where strict equality operator comes to the rescue.

Strict equality operator (“===”)

Compares the data type and value of the operands. The strict equality operator evaluates the statement as false if the operands are of different data types. If the data type of both operands is the same only then we compare the values. So all the rules we saw above for equality(==) don’t hold good here.

1 === “1”     //false
“” === 0 //false

In javascript, two NaNs can not evaluate to the same value.
For example, 0/0 == 9/0 evaluates to false (NaN == NaN is false).
The null and undefined can not be converted into Number type.
This rule holds good for both the equality operator.

Comparing javascript objects

The javascript objects comparison is based on the reference. Hence, if two objects refer to the same location they result in true. Object comparison works the same in both the equality operator.

let a={x : 1}
let b={x : 1}
let c=a
a==b //false
a==c //true
a===b //false
a===c //true

It is always a good idea to use the strict equality operator(===), unless there’s a specific requirement to use the loose equality operator (==). Strict equality operator provides a predictable result and you don’t have to worry about the rules. This way you can avoid unexpected results and bugs in your code and that’s always a bonus.

--

--