Difference Between =, ==, and === in JavaScript with Examples

Introduction :

In this tutorial will learn Difference Between =, ==, and === in JavaScript with Examples. In Javascript, we have couple of options for checking equality. we’ll explore here similarities and difference between = (single equal) and ==(double equals) and === (triple equals) in JavaScript

  • = (Single equal): Known as assignment operator it always assign value to variable.
  • == (Double equals): Known as the equality or abstract comparison operator
  • === (Triple equals): Known as the identity or strict comparison operator

What is =(equal to) in JavaScript?

Equal to (=) is an assignment operator, which sets the variable value on the left of the = to the value of the expression that is on its right.

For example, if we have variable var A and want to assign value 10 to var A then will write below syntax:

  var A= 10;

What is == (double equal to) in JavaScript?

Double equals operator (==) is known as comparison operator or equality operator or abstract comparison operator.

So, when you compare string with a number, JavaScript converts any string to a number. An empty string is always converts to zero. A string with no numeric value is converts to NaN (Not a Number), which returns false.

==(double Equals operator) only compare value of two variable. it only checks for equality of two variable value without checking data type.

What is === (triple equals) in JavaScript?

=== (Triple equals) is a strict equality comparison operator in JavaScript, which returns false for the values which are not of a similar type. This operator performs type casting for equality. If we compare 10 with “10” using ===, then it will return a false value.

The Difference between == Double equals Operator) and === (Triple equals Operator):

  • == converts the variable values to the same type before performing comparison. This is called type conversion.
  • == converts the variable values to the same type before performing comparison. This is called type conversion.

Let’s take an  example:

var A = 1;
var AB = 1;
var A_string = “1”; // note: this is string data type

console.log(A == AB);         // true
console.log(A === AB);      // true
console.log(A == A_string);         // true
console.log(A === A_string);   // false

line 4 : console.log(A == AB) returns true because both variable values are same.

line 5 : console.log(A === AB) returns true because both variable values and data type are same.

line 6 : console.log(A == A_string) returns true because both variable values are same without type checking.

line 7: console.log(A === A_string) returns false because both variable values are same but datatype are different.

KEY DIFFERENCES:

== (Double Equals Operator) === (Triple Equals Operator)
it is used for comparing two variables, but it ignores the datatype of variable. it is used for comparing two variables, but this operator also checks datatype and compares two values.
it is comparison operator it is also comparison oprator
it checks equality of to variable without checking data type it checks quality of two variable with considering the datatype
==make type correction based upon values of variables. === takes type of variable in consideration.
== checks for equality only after doing necessary conversations. two variable values are not similar, then === will not perform any conversion.

Also Learn More Tutorial :

Leave a Reply

Your email address will not be published.