JavaScript: Difference between = vs == vs ===
Nashmeyah
Posted on December 7, 2020
Why is the purpose of using = in JavaScript?
As you're programming, when you want to assign value to a variable to use or change throughout the code, you use the = operator. The = operator is called the assignment operator, and there are many ways you can assign values to variables.
Example:
let x = 10;
let name = "Jhonny";
let nameList = [ "Lisa", "Mona", "Tim"]
If = means assigning values, what does == mean?
Now think if you want to compare x to name, how would you compare them to evaluate if the variables are of the same value. That is where the == operator comes in handy, this operator called the equality operator. The return value when using it is a Boolean(true or false), it is not strict so if you compare
1 == "1"; // true
x == "10"; // true
name == x; // false
the return values vary. Not strict equality means that the equality operator will try to convert the values datatype before comparison happens.
What is strict equality?
Similar to the == equality operator, we have === strict equality operator. This works similarly except without trying to implicitly convert the variables values, it compares it strictly to the value and the value datatype and also returns a Boolean.
For example:
x === 10; //true
x === "10" // false
name === "Lisa" //false
If the values are not similar, it will not convert them therefore the result will false.
I hope this helps, I wanted to make it very simple and down to the basics.
Posted on December 7, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.