difference between =,== and === in javascript

=

==

===

are these 3 the same operators? arent they all the same equal signs? is there any difference?

these 3 operators may seem similar at face value but in javascript they have some intriguing differences. lets take a look at them one by one.

the "=" operator

this is called the assignment operator. in javascript its used to assign a value to a variable and doesnt return booleans(i.e true or false) an example code snippet of how it is used:

let bill = 300;

this assigns the variable "bill" the value of "300" as can be seen when you log it to the console:

let bill = 300;
console.log(bill);

this would print out the value "300" to the console.

the "==" operator

this is known as the comparison or equality operator. in JavaScript is used for comparing two variables, but it ignores the datatype of variable. so basically, it Checks the equality of two operands without considering their type. this usually comes down to the operator not caring if the value passed is a string or integer, as long as they are of the same input value. it Returns true if the two operands are equal. It will return false if the two operands are not equal.

a code snippet example:

let x=5;
x == 8;  //returns false
x  == 5; //returns true
x == "5"; // returns true

on returning the values for the above, we see the operator returns true when equated with the string value of 5. this clearly shows the datatype is not taken into consideration when using this operator.

the "===" operator

much similar to the "==" operator this is also called the comparison operator. it is used for comparing two variables, but this operator also checks datatype and compares two values.

a code snippet:

let x=5;
x === 8;  //returns false
x  === 5; //returns true
x === "5"; // returns false

as expected , this operator takes into account the datatypes of the variable and compares it with the value given. this is the major difference between "==" and "===", you could say "===" is the more strict version of "==".

these are the key differences between the assignment operator("="), the equality/comparison operator("==") and the comparison operator("===")