What is the difference between == & === in JavaScript?

Photo by Andrew Neel on Unsplash

What is the difference between == & === in JavaScript?

In JavaScript, = is used as the assignment operator and === is known as the strict equality operator. Understanding the difference between these two operators is crucial for writing effective JavaScript code. In this blog, we will explore the key differences between = and ===, and how to choose the right operator for your needs.

Assignment Operator (=)

The assignment operator = is used to assign a value to a variable. For example, the following code creates a variable x and assigns the value 5 to it:

let x = 5;

The value of the right-hand side of the assignment operator (5) is stored in the left-hand side variable (x). The value of x can be changed later in the program by assigning it a different value, as shown below:

x = 10;

The assignment operator is also used to assign a value to a property of an object. For example:

let person = {};

person.name = "Dave";

In this example, the name property of the person object is assigned the value "Dave"

Strict Equality Operator (===).

The strict equality operator === is used to compare two values to see if they are equal in both type and value. In other words, === checks if both values are of the same type and contain the same data. The following code demonstrates the use of the strict equality operator:

let x = 5; let y = "5";

console.log(x === y); // false

In this example, the values 5 and "5" are of different types (number and string, respectively), so the comparison x === y returns false.

The strict equality operator can also be used to compare objects, arrays, and functions, as shown below:

let obj1 = {};

let obj2 = {};

console.log(obj1 === obj2); // false

let arr1 = [];

let arr2 = []; console.log(arr1 === arr2); // false

function myFunc() {}

function myFunc2() {}

console.log(myFunc === myFunc2); // false

In each of the examples above, the two objects, arrays, or functions are compared, and since they are two separate entities in memory, the comparison returns false.

Choosing the Right Operator

When writing JavaScript code, it's important to choose the right operator based on your needs. Here are some guidelines to help you make the right choice:

Use = for Assignments

Use the assignment operator = when you want to assign a value to a variable or a property of an object. It's simple, straightforward, and easy to understand.

Use === for Comparisons

Use the strict equality operator === when you want to compare two values for equality in both type and value. This operator helps to avoid type coercion and ensures that the comparison is as accurate as possible.

Consider Using == for Loose Equality Comparisons

While === provides a strict equality comparison, there are cases where you may want to perform a loose equality comparison instead. In these cases, you can use the loose equality operator ==, which compares two values for equality in type and value, but will