Difference Between =,==,=== in JavaScript Explained

spot_img

Share This Post

When you start to learn how to program in JavaScript, you might see code that uses three different operators that look very similar.

The = (equals), == (double equals), and === (triple equals) operators are all important in JavaScript, and they all do different things. Let’s look at the difference between the JavaScript symbols =, ==, and ===.

When to Use = in JavaScript?


The Javascript operator equal (=) is very different from double equals (==) and triple equals (===). This is what is called an “assignment operator,” and it is used to give values to variables.

var primary = "Hello";

In this case, we used the = (equal) operator to give the variable primary the value “Hello.”

 

When to Use == in JavaScript?


Both the double equal (==) operator and the triple equal (===) operator are comparison operators in JavaScript.

The double equal (==) operator, on the other hand, is used to find abstract equality. This means that when double equal (==) is used, it first changes the types of the two values and then compares them.

The following example will evaluate to true because the double equal operator will do something called “Type Coercion” to convert both values to the same type and then compare them.

if (100 == '100') {
    //This will be equal
}

Your program will tell you when to use this operator. If you think that changing types will break the code, you might want to use the === operator.

 

When to Use === in JavaScript?


In JavaScript, the Triple Equal (===) operator is a strict comparison operator. This operator is used to compare two values while taking into account their types. If you used the first example, you’d get the opposite result.

if (100 === '100') {
    //Not considered equal
}

Now you know the difference between JavaScript operators “=”, “==”, and “===”. Happy coding!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related Posts

Learn How to Select All in VIM?

Now is the time to improve your VIM skills...

Learn How to Modify Color Syntax in Notepad++

Since it came out in 2003, Notepad++ has become...

How to Fix replaceAll is not a function in Node JS?

One of the most used string prototypes in JavaScript...

Learn How to Set the Viewport in Puppeteer?

One of the most popular headless chrome APIs for...

Learn How to Reverse Order of a ForEach Loop in PHP?

As a programmer, you'll quickly learn how to work...

How to Get Elements between Indexes in Javascript Array?

If you want to be a good programmer, you...
spot_img
- Advertisement -spot_img