DEV Community

Nashmeyah
Nashmeyah

Posted on

JavaScript: Difference between = vs == vs ===

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"]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)