The JavaScript Assignment, Equality, and Strict Equality operators
Published: 2023-05-15 14:01:48

---
title: The JavaScript Assignment, Equality, and Strict Equality operators
published: true
date: 2022-08-28 21:48:30 UTC
tags: 
canonical_url: https://hayatscodes.hashnode.dev/the-javascript-assignment-equality-and-strict-equality-operators-e56aaded4bfd
---

The equal (=), double equal (==), and triple equal (===) signs, which are respectively known as the assignment, equality, and strict equality operators in JavaScript, often confuse junior JavaScript developers. Inappropriate use of these operators can make programs run abnormally, sometimes without the compiler throwing an error, making codes difficult to debug.

In this article, I will explain what each of them does and how and when to use them.

**Lets dive in!**

### The Assignment Operator (=)

The assignment operator is the same across all programming languages. As its name implies, in the simplest form, it is used to assign values to variables. The variable is on the left-hand side and the assigned value is on the right-hand side.

It can also be combined with the arithmetic operators to perform simple operations before assignment. For more on this, click [here](https://www.educba.com/javascript-assignment-operators/).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672560968973/uT9743qE5.png)

The above image illustrates the usage of the assignment operator and its shorthand forms.

It was coded as follows:

- Line 1 declared a variable called `newVariable`_._

- Line 2 assigned the value 20 to `newVariable`.

- Line 36 syntax is `newVariable = newVariable <op> 10`_. Where_ `<op>` _is replaced with the appropriate operator, e.g., line 3 is the same as_ `newVariable = newVariable + 10` **_._**

The assignment operator has [right-to-left associativity](https://www.telerik.com/blogs/details/operator-precedence-javascript#:~:text=Operator%20Precedence%20Operator%20precedence%20in%20JavaScript%20determines%20the,the%20order%20to%20perform%20a%20given%20mathematical%20expression.), that is, operations are performed from right to left. Thus, line 3 of the code will add `newVariable` to 10 before assigning the result back to `newVariable` _(itself)_.

### The Equality Operator (==)

The equality operator, which has a double equal sign, is also similar across most programming languages.

The operator is used to compare two or more expressions and always evaluates to a true or false (boolean) value.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672560970946/kzUGt2pX_.png)

In the above code:

- I assigned 100 to variable `num1` and 300 to variable `num2`, then

- `num1` and `num2` variables are compared and at the same time stored in a result variable.

- And a false value is logged to the console since the values of `num1` and `num2` are not equal.

Before comparison, the operator also performs type conversion if necessary.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672560972459/qkj1lCvmh.png)

The above code compares a variable string of 10 with a variable number of 10 using the equality operator, and a true value is obtained, though theyre of different types.

There is also an inequality operator (!=) in JavaScript which does the exact opposite of the equality (==) operator.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672560974211/A1zPO-7FY.png)

Changing the equality operator in the above code to an inequality operator, a truthy value is obtained.

### The Strict Equality Operator (===)

The strict equality operator, unlike the other two operators, is only available in two programming languages: PHP and JavaScript.

The operator works like the equality operator, except that it does not make a type conversion before comparison.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672560975482/8njhRymGq.png)

In the above diagram, a comparison using the strict equality operator, though having a value of 10 of different types, evaluates to false.

Using the strict equality operator is considered as best practice to avoid code bugs.

The strict inequality operator (!==) in JavaScript does the exact opposite of the strict equality (===) operator.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1672560976654/BsxougyCF.png)

The above code checks if string 50 is not equal to number 50, using the strict equality operator, and it evaluates to true.

Check [here](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) for more on the difference between equality and strict equality operators.

#### Conclusion

In this article, I explain the difference between the assignment, equality, and strict equality operators. You should now be able to distinguish between the three operators as well as their use cases.