Difference between == and === in Javascript​

Introduction of Assignment (=), Equality (==), Strict Equality (===) Operators and the difference between == and === in Javascript

As we know in javascript many operators are being used like Arithmetic Operators, Logical Operators,  Assignment Operators, Bitwise Operators, Comparison Operators, Ternary Operators, String Operators and Type Operators. Out of which Assignment & Comparison Operators are dear to interviewers. 

As we know, the Difference between == and === in Javascript Operators is one of the favorite questions of the interviewer. These operators are widely useful in assigning values and for comparisons.

In the dynamic realm of JavaScript, knowing the nitty gritty details between == and === is crucial for writing robust and bug-free code. These seemingly similar equality operators play distinct roles in comparing values, and skillfully practicing their differences can impact your code’s reliability. While == performs type coercion, converting operands to the same type before comparison, === enforces strict equality without type conversion. In this exploration, we’ll unravel the intricacies of these operators, empowering you to make informed decisions when comparing values in your JavaScript projects.

1) = Operator in Javascript first step to know the Difference between == and === in Javascript

(=) javascript is called an assignment operator.

Let’s consider the following example:

age = 10

Here, the value of number 10 is assigned to the variable ‘age’.

Please note:

To store the values in memory, JavaScript language is different from other languages like C and C++. In C and C++ language they have memory addresses to store and manage the data. So, they have Lvalue and Rvalue concepts in them.

But since Javascript uses a garbage collector it does not have any Lvalue & Rvalue concept.

Memory Usage C, C++ Vs Javascript

2) == Operator in Javascript second step to know the Difference between == and === in Javascript

To compare both operands JavaScript uses the (==) operator or it is called a JavaScript comparison == operator too. 

In order to understand the difference between == and === in Javascript we need to know that Double equals (==) converts its data type into values before comparing both operand’s values. This is also called coercion in JavaScript. 

For e.g., 10 == ‘10’ returns True 

Here, the number 10 is compared with the string ‘10’. String datatypes convert into numbers first and then it compares with the number 10 which will return a boolean value true. 

A few scenarios make you feel awkward while knowing (==) JavaScript operator and are mentioned as follows:

3) === Operator in Javascript third step to know the Difference between == and === in Javascript

Now (===) operator in JavaScript is called a Strict Equality Operator or Strict Equality Comparison Operator. === operator does not hesitate to return false if operands belong to different data types.  When it comes to knowing the difference between == and === in Javascript we can conclude and say that Strict Equality (===) operator is more reliable to use.

JavaScript operator === returns Boolean value i.e., True/ False 

FAQ on the Difference between == and === in Javascript

Why and when we should avoid using == Javascript Comparison Operator?

a. null == undefined 

Returns ‘True’ as a result. You are kidding! Is it? 

b. 0 == ‘’ 

This will return ‘True’ which is quite surprising though keeping in mind that empty sting does now hold any value 

c. 0 == false  

Returns true since the datatype of Number 0 implicitly converts into the datatype of Boolean. 

From the above explanation, we would want to know the difference between == and === in Javascript to choose precise operator not only for programmers but will be more relaxing for testers… LOL! 

Why do we use === instead of ==?

Data Type coercion is implicitly done in the Double Equals ( == ) javascript operator prior to comparing values. Arguably this is the reason to use the == operator cautiously in order to avoid unexpected errors.

Whereas Triple Equals ( === ) does not do any data type conversion (coercion) during comparison.

So knowing the Difference between == and === in Javascript Operators can help us to avoid unexpected errors.

Difference between == and === in Javascript​

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top