HomeGuidesSnippets

JS equality

August 11, 2021

post-bg

Content
Equality
Comparing objects
Type coercion
Double & triple equality

Equality

JavaScript is a dynamically typed language, meaning that you don't have to worry about having to specify the type of variable. Type is determined at run-time by the interpreter, and it can be changed by the user even after it has been initialized.

let numFive = 5;                //typeof 'number'
numFive = numFive.toString();   //typeof 'string'

Objects stored by reference an object is strictly equal to another, only if they both point to the same object in memory.

Comparing two objects

Obj1 makes reference to a space in memory. Obj2 is equal to obj1, thus makes reference to the same obj1. Obj3, although the same prop/value as obj1 and obj2, it points to a different space in memory.

const obj1 = {num: 5};
const obj2 = obj1;
const obj3 = {num: 5};

//returns true - same reference
obj1 === obj2

//false - same key/value but different reference
obj1 === obj3

Objects shallow comparison

Here below, a function for comparing two object's keys and values. As long as none of the values happen to also be an object - nested - the function does okay.

function objectsEqual(o1, o2) {
  const entries1 = Object.entries(o1);
  const entries2 = Object.entries(o2);
  if (entries1.length !== entries2.length) {
      return false;
  }
  for (let i = 0; i < entries1.length; ++i) {
      // Keys
      if (entries1[i][0] !== entries2[i][0]) {
      return false;
      }
      // Values
      if (entries1[i][1] !== entries2[i][1]) {
      return false;
      }
  }

  return true;
}

Deep equality

Compare objects that contain other objects within their values. There isn't a one-fit-all solution for deep equality in JavaScript.
One way to find out whether two nested objects are equal or not is with JSON.stringify - although, it's not guaranteed to always work as expected. For those special cases where your life may depend on it, there is a JS library called Lodash, that should further increase your odds for deep equality.

const obj1 = {num: 5};
const obj3 = {num: 5};

// returns false
JSON.stringify(obj1) === JSON.stringify(obj3)

Comparing primitive data types

Type coercion

Refers to the implicit conversion of values from one data type into another: Number to String or Boolean to Number, etc.
In this context, implicit means that the interpreter does it automatically at run-time. In double equals comparisons, it changes the second value's type, to match the type of the first value.

Double equality

In the example below, the interpreter first changes var b data type from string to number, and then it proceeds to compare the two.

var a = 7;
var b = '7';

//comapre these two
7 == '7'   //returns true
false == 0   // returns true

Whenever comparing two primitive data types using double equals, the interpreter first makes sure both are of the same type - type coercion - and then does the comparison.

Triple equality

Also referred as strict equality. It only returns true when both type and value are equal to one another.
In the example below, there's no type check because of the triple equals, and the interpreter returns false because var a
is of type number and var b is of type string.

var a = 7;
var b = '7';

//comapre these two
7 === '7'   //returns false

'hello' === 'hello' // returns true
'20' === '20'  // returns true
false === 0   // false
  • Terms & Conditions
  • kcosa.com © 2022
  • Privacy Policy