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

Javascript Difference Between = == ===

When you start programming in JavaScript you might begin to start seeing code using three different operators that all look very similar. The = (equals) operator, the == (double equals), and the === (triple equals) are all important operators in JavaScript and they all serve different purposes. Let’s take a look at the difference between =, ==, === in JavaScript. 

When to Use = in JavaScript?

Equal (=) in Javascript is a very different operator compared to double equals (==) and triple equal (===). This operator is what is known as an assignment operator is used to assign variables values. 

var primary = "Hello";

In this example, we used the = (equal) operator to assign a value of “Hello” to the variable primary. 

When to Use == in JavaScript?

Double equal (==) in Javascript is similar to the triple equal (===) operator because they are considered comparison operators. However, the == (double equal) operator is used to find abstract equality. This means that double equal (==) performs a type conversion for use and then compares the two values. 

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

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

When to use this operator is based on your program. If you foresee type conversion as code-breaking, you might want to use the Triple Equal (===) operator. 

When to Use === in JavaScript?

The Triple Equal (===) operator is considered a strict comparison operator in JavaScript. You would use this operator when you want to compare two values and take the types into account. Using the previous example, you would get the opposite response. 

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

Now you know the different between “=”,”==”,”===” operators in JavaScript. Happy coding!

Check out some of our other Javascript guides.

Disclaimer: Some posts on Techozu may contain affiliate links. Purchasing through these links may earn us an affiliate commission.