Difference between =, ==, and === in JS

Sreelal TS
3 min readOct 17, 2018

--

I think almost all JavaScript beginners would have asked this question, “What is the difference between =, ==, and === in JS?”. Anyway, I’ve been receiving this question repeatedly. Through this article, I’ll demonstrate to you the comparison of these three operators.

Simply, we can define these operators like this:

  • = is the assignment operator. Used to assign values to variables.
  • == is a comparison operator, it will compare two elements irrespective of their datatype.
  • === is also a comparison operator. But, it will compare two elements by value and datatype, strictly.

Let’s go deeper…

See this example of the = operator:

That’s how the = operator used to assign values to a variable. The variable name is given at the left side of the operator and the value to be assigned is to be given at the right side of the operator.

Now, let’s check the == operator, here is an example:

In this example, the variables favNo and elem are assigned to 89. And I used the == operator to check whether my variables are equal. See the output down here:

And the result was true. So, that’s the use of the == operator. It is used to compare two variables and return a boolean value.

What about the === operator? Let’s figure it out. I’ve told you that the === operator is used to check the equality in both value and datatype of two variables. It will return true if and only if both datatype and the value is exactly the same.

Here is an example:

Sure, it will return true, because both variables are of integer datatype and value is 89.

Let’s check another one example:

This result falls to false. Why? Simply, we can say that the variable favNo is an integer variable with value 89 and the variable elem is a string variable with content “89” which are exactly two different kinds.

Let’s check the same with == here:

It evaluates to true! Because you know they have the same value!

That’s it! Now, I hope you got the difference between these three operators. You only have to remember, the = is for assignment, the == is for checking variables by value only, and the === if for checking equality between variables by value and datatype.

Thank you :)

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Sreelal TS
Sreelal TS

Written by Sreelal TS

codes • dreams • thoughts . 💙 Organizer, Flutter Kozhikode 🥇 Platinum Product Expert at Google PE Program #Flutter #GDGKozhikode

No responses yet

Write a response