Differences between ‘=’, ‘==’, and ‘===’ operators in JavaScript

The Confused Creative
2 min readApr 12, 2021

--

Note: This article has been simplified for JavaScript beginners to understand.

The equal to ‘=’, double equal to ‘==’ and triple equal to ‘===’ are operators in JavaScript.

What is ‘=’ in JavaScript?

Equal to ‘=’ is an assignment operator, by assignment we mean, giving value or assigning value to something. It assigns the value of one operand to another. That is, a ‘=’ 1 assigns the value of 1 to a. ‘=’ does not return true or false unlike the other comparison operators.

Examples of ‘=’

x = 1 (x now equals 1)

x = 1 (x now equals 2)

y = 7 (y now equals 7)

What is ‘==’ in JavaScript?

Double equals to ‘==’ is a comparison operator that checks the comparison between two variables to know if they’re equal, it transforms the operands having the same type before comparison. The ‘==’ operator tests for abstract equality meaning it does the necessary type conversions before doing the equality comparison. ‘==’ will try and convert one side of the expression to be the same type as the other. Therefore, when you compare a string with a number, JavaScript converts the string to a number.

Examples of ‘==’

1==true (true)

3==3//true

“3”==3//true

3==’3'//true

What is ‘===’ in JavaScript?

The ‘===’ is a comparison operator that tests for strict equality, it will not do the type conversion hence if the two values are not of the same type, when compared, it will return false. When using ‘===’, it does not try and convert before doing the comparison, it checks if true equals 1, which doesn’t as they are two different data types, and the result returns false.

Examples of ‘===’

3 === ‘3’ (false because 3 and ‘3’ are of different types, === doesn’t convert ‘3’ to a number)

8 === 8 (true because 8 and 8 are of the same types)

Note the important differences: ‘=’ assigns values to variables, ‘==’ compares two variables without checking their data types and ‘===’ also compare two variables but checks the data types.

To round up this article, it is important to note that, none of these operators can be used interchangeably, each operator gives a different result, so be sure of exactly what you want to achieve and know which one of the operators to use.

--

--

The Confused Creative
The Confused Creative

Written by The Confused Creative

Multifaceted Creative. MERN stack Developer. Certified PT

No responses yet