DEV Community

Nashmeyah
Nashmeyah

Posted on

2 1

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.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay