JavaScript

What is the Difference Between =, == and === in JavaScript?

JavaScript is a programming language that allows us to create and develop web applications and web pages as well as make our websites more dynamic/interactive. Data can be calculated, manipulated, and validated using JavaScript.

Like any other language, JavaScript has operators. An operator produces a result by performing some action on a single or multiple operands (data value). Let’s look at an example of 2+2 where the numbers are left and right side operands and the + is the operator. This + operator adds the two numbers together.

With examples, we’ll examine and answer the question that what is the difference between the =,==, and === operators in JavaScript in this article.

What is = operator?

The = sign or equal to the operator is an assignment operator. The function of equal to is to set the value of the left-hand side to the right-hand side. For example a=10. In this example, the variable is set to the number 10. However, it should be noted that 10=10, ‘a’=’a’ will give a reference error as we are kind of comparing them instead of setting a value.

Example of =operator

var num1=10;
var num2=5;
alert(num1+num2);

Num1 and num2 variables are assigned values of 10 and 5and after this, we add both num1 and num2 and alert the result which can be seen in the above screenshot.

What is == operator?

The == sign or Double equals function is called the equality operator and its function is to compare two values or expressions (compare strings, variables, numbers, etc). The result is true if the expressions are equivalent; otherwise, the result is false.

However, it should be kept in mind that boolean, numbers, and string values are compared by value and not by reference hence If they have the same equivalent value then it is considered true. However, variables, functions, objects, and arrays are compared using reference, for example, two variables are only equal if they refer to the same object, function, or array.

Example of == operator

var num1=10;
var num2=5;
alert(num1==num2);

In this example, we took two variables and assigned 10 and 5 numbers to them. Then we alert by checking whether num1 and num2 are equal which results in a false statement as == operator checks by value.

Now we change the code a little bit i-e assigned the value of 10 to num2 and then checked whether num1 and num2 are equal and it is as it returns a true value.

var num1=10;
var num2=10;
alert(num1==num2);

What is === operator?

The === sign or triple equals is called the strict equality operator and its function is to check whether the two operands are equal or not and return a true or false value. === operator returns false when the values are not of similar type and true when they are of similar type. To simplify we can say that the === operator performs typecasting, for example, let’s compare 1===”1” this will return false as the values are the same but the type is not the same.

var num1=10;
var str1="10";
alert(num1===str1);

As discussed earlier, the === operator checks both the value and the type of two operands so we took a num1 and assigned an integer 10 to it. Then we took str1 and assigned a string of number 10. We then alert whether num1 and num2 are equal or not which returns as false as although the values are the same but the type is not the same, one being an integer and the other a string.

Now we change the above code a little bit and initiated num2 with a value of 10 to it. We then alert whether num1 and num2 are equal which returns true as the values and the datatype are both same in num1 and num2:

var num1=10;
var num2=10;
alert(num1===num2);

Conclusion

JavaScript is a programming language used to build web applications and JavaScript offers operators which help in the calculation, manipulation, and validation of data.  Operators perform some action on operands and return the result. In this article, we defined =,== and === operators in JavaScript and gave examples of these operators to shine the light on the differences among them.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.