Monday, 6 January 2025

Strict Equality (===) and Strict Inequality (!==) Operators in JavaScript

The strict equality (===) and strict inequality (!==) operators are used in JavaScript to compare two values. They are known as strict operators because they do not perform type coercion, meaning that both the value and the type must be the same for the comparison to return true or false.

1. Strict Equality (===) Operator

The === operator checks if two values are equal in both value and type.

It returns true if both the value and the type are the same; otherwise, it returns false.

 

Example 1: Comparing Numbers

let a = 5;
let b = 5;
console.log(`a : ${a}, b : ${b}, a === b : ${a === b}`);

 

‘a === b ‘ is evaluated to true, because both values are 5 and both are of type number

 

Example 2: Comparing Different Types

a = 5;
b = "5";
console.log(`a : ${a}, b : "${b}", a === b : ${a === b}`);

 

‘a===b’ is evaluated to false, because 5 is a number and "5" is a string

 

Example 3: Comparing Booleans

let isActive = true;
let isLoggedIn = true;
console.log(`isActive : ${isActive}, isLoggedIn : ${isLoggedIn}, isActive === isLoggedIn : ${isActive === isLoggedIn}`);

 

isActive === isLoggedIn is evaluated to true, because both are true and both are of type boolean.

 

Example 4: Comparing Objects.

let obj1 = { name: "Alice" };
let obj2 = { name: "Alice" };
console.log(`obj1 : ${JSON.stringify(obj1)}, obj2 : ${JSON.stringify(obj2)}, obj1 === obj2 : ${obj1 === obj2}`);

‘obj1 === obj2’ is evaluated to false, because although the objects look the same, they are different instances in memory

 

Find the below working application.

 

strictEqualityOperator.js

let a = 5;
let b = 5;
console.log(`a : ${a}, b : ${b}, a === b : ${a === b}`);

a = 5;
b = "5";
console.log(`a : ${a}, b : "${b}", a === b : ${a === b}`);

let isActive = true;
let isLoggedIn = true;
console.log(`isActive : ${isActive}, isLoggedIn : ${isLoggedIn}, isActive === isLoggedIn : ${isActive === isLoggedIn}`);

let obj1 = { name: "Alice" };
let obj2 = { name: "Alice" };
console.log(`obj1 : ${JSON.stringify(obj1)}, obj2 : ${JSON.stringify(obj2)}, obj1 === obj2 : ${obj1 === obj2}`);

Output

a : 5, b : 5, a === b : true
a : 5, b : "5", a === b : false
isActive : true, isLoggedIn : true, isActive === isLoggedIn : true
obj1 : {"name":"Alice"}, obj2 : {"name":"Alice"}, obj1 === obj2 : false

Strict Inequality Operator (!==)

The !== operator checks if two values are not equal in either value or type. It returns true if either the value or the type is different; otherwise, it returns false.

 

strictInequalityOperator.js

let a = 5;
let b = 5;
console.log(`a : ${a}, b : ${b}, a !== b : ${a !== b}`);

a = 5;
b = "5";
console.log(`a : ${a}, b : "${b}", a !== b : ${a !== b}`);

let isActive = true;
let isLoggedIn = true;
console.log(`isActive : ${isActive}, isLoggedIn : ${isLoggedIn}, isActive !== isLoggedIn : ${isActive !== isLoggedIn}`);

let obj1 = { name: "Alice" };
let obj2 = { name: "Alice" };
console.log(`obj1 : ${JSON.stringify(obj1)}, obj2 : ${JSON.stringify(obj2)}, obj1 !== obj2 : ${obj1 !== obj2}`);

Output

a : 5, b : 5, a !== b : false
a : 5, b : "5", a !== b : true
isActive : true, isLoggedIn : true, isActive !== isLoggedIn : false
obj1 : {"name":"Alice"}, obj2 : {"name":"Alice"}, obj1 !== obj2 : true



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment