Difference Between =, ==, and === in JavaScript

Difference Between =, ==, and === in JavaScript

Introduction to the operators

  • = is used for assigning values to a variable in JavaScript.
  • == is used for comparison between two variables irrespective of the datatype of the variable.
  • === is used for comparison between two variables but this will check strict type, which means it will check the datatype and compare two values.

Each operator explained more in detail

=

= is an assignment operator used to assign a variable to a value. Example

let a = 25 // = there, assigns 25 to a

==

== is used for comparing two variables, but it ignores the datatype of the variable.

20 == "20" // the result will be true even if the data types are different.

===

=== is used for comparing two variables, but this operator also checks datatype and compares two values.

20 === "20" // the result will be false, because the data types are different.

Key Differences

1) = is used for assigning values to a variable, == is used for comparing two variables, but it ignores the datatype of variable whereas === is used for comparing two variables, but this operator also checks datatype and compares two values.

2) = is called as assignment operator, == is called as comparison operator and === is also called a comparison operator.

3) = does not return true or false, == Return true only if the two operands are equal while === returns true only if both values and data types are the same for the two variables.

Conclusion

So, in conclusion, = is used to assign a value to a variable( assignment operator), == checks if two values are equal, regardless of their data type. === is also an assignment operator and checks two values, but it checks their data types and it gives false if their data types are different even if their values are equal.

Thanks for reading.