strict equality code example

Example 1: strict equality

//Strict equality operator

console.log(1 === 1);
// expected output: true

console.log('hello' === 'hello');
// expected output: true

console.log('1' ===  1);
// expected output: false

console.log(0 === false);
// expected output: false


//Comparing operands of the same type

console.log("hello" === "hello");   // true
console.log("hello" === "hola");    // false

console.log(3 === 3);               // true
console.log(3 === 4);               // false

console.log(true === true);         // true
console.log(true === false);        // false

console.log(null === null);         // true

//Comparing operands of different types

console.log("3" === 3);           // false

console.log(true === 1);          // false

console.log(null === undefined);  // false


//Comparing objects

const object1 = {
  name: "hello"
}

const object2 = {
  name: "hello"
}

console.log(object1 === object2);  // false
console.log(object1 === object1);  // true

//Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality

Example 2: javascript equality

18 === 18 // true
18 === 19 // false
`18` == 18 // true
`18` === 18 // false

// == : loose equality operator (date type IS NOT relevant)
// === : strict equality operator (date type IS relevant)

// Note: the same applies for not equal
`18` != 18 // false
`18` !== 18 // true

Example 3: strict equality operator

/**
* Strict equality operator (===) checks if its two operands are identical.
*
* The 'if' statement below will yield to false. 
* 
* This is because the strict equality operator checks if both the data type AND the value contained are
* the same.
*/
let x = 8
let y = "8"
if (x === y)