Custom Implementation of JavaScript Methods
Tags: comparison, coding, javascript, programming
Published: 2023-05-10 12:32:44

---
title: Custom Implementation of JavaScript Methods
published: true
tags: comparison,coding,javascript,programming
canonical_url: 
---

#### Part 2: Custom implementation of shallowCompare() and deepCompare() with the typeof operator in JavaScript.

![img1](https://cdn-images-1.medium.com/max/396/1*TMswwB3rNiQyyo35hYfyzw.png)

### **Deep/Shallow comparison with typeof operator**

Hi guys, today I am going to show you how to write the custom implementation for the shallow and deep copy with the use of the custom typeof operator.

**Let’s get started.**

First, we need to know what are Shallow and Deep comparisons. These terms were quite intimidating when we were asked in the interviews but once you get an understanding of the JavaScript runtime environment, their meaning becomes quite simple.

Let me tell you that the comparison in JavaScript between primitive types and non-primitive types is quite confusing in some parts but if you know exactly what might be the difference between primitive and non-primitive types in JavaScript, these would be quite easy to answer and explain to anybody else.

#### What are primitive types and non-primitives types in JavaScript?

_Primitive data types are data types that are not any object and have no properties or methods attached to them._

**There are 7 primitive data types:**

- string
- number
- bigint
- boolean
- undefined
- symbol
- null

One important point related to primitive data types is all primitives are _immutable_; that is, they cannot be altered.

If you need to know more information about the primitive data types, just visit: [https://developer.mozilla.org/en-US/docs/Glossary/Primitive](https://developer.mozilla.org/en-US/docs/Glossary/Primitive)

_Non-primitive data types are known as derivatives (reference type) which means it is derived from the primitive data types only._

- Arrays
- Objects

Now that we have a little understanding of what primitive and reference data types are, let’s move on to the topic of deep and shallow comparison.

#### 1. shallowCompare()

Shallow comparison is a type of comparison which checks if two values are equal. It should be checked on one level which means nested object references are not ready for comparison, only one level of comparison between primitive and non-primitive data types is allowed.

![img2](https://cdn-images-1.medium.com/max/1024/1*VWokWO1hGb2xzvObXAoKEQ.png)
_Shallow comparison on 1st level_

This figure states that if there is a shallow comparison happening it should happen correctly at one level, not at the nested level.

![img3](https://cdn-images-1.medium.com/max/935/1*ogEWHhmZbJzNSl7liWrenQ.png)
_Shallow comparison is not allowed on nested references._

_Before we jump into the_ **_custom implementation of shallow comparison_** _we need to do one more custom implementation of the_ **_typeof Operator_** _(a built-in JavaScript method) — you can use the_ _typeof operator to find the data type of a JavaScript variable._

![img4](https://cdn-images-1.medium.com/max/632/1*VbNJx1lGNNknfaGsfilqGw.png)
_Inbuilt type of operator_

_\*\* One major drawback or pitfall which I think is in JavaScript is that it will return the type of object for all the reference types such as an array, object, date, null, etc._

![img5](https://cdn-images-1.medium.com/max/552/1*N6RS5chqhCGWjhda99wVMQ.png)

_So if you need to know whether it is an array or an object, it won’t be possible. It always returns an object which is quite confusing but, after all, it was designed by the JavaScript guys keeping in mind that an array is treated like an object, due to its nature of dynamic typing in JavaScript.\*\*_

**_So, to achieve functionality to showcase whether it is an array or an object, or a date, we need to write our custom typeof operator._**

Let me show you what it looks like:

#### **_Custom Implementation — typeof operator_**

![img6](https://cdn-images-1.medium.com/max/886/1*W_KDCoyVUI8st1WtEYka3A.png)
_Custom TypeOf operator_

Now we get the exact type of the reference data types that we pass, whether it is an array, an object, or a date.

Before making any further delay, let’s concentrate on the shallow comparison.

#### **Custom Implementation — shallowCompare()**

![img7](https://cdn-images-1.medium.com/max/727/1*LvsVNAstYc0UAAZ7AyE6aA.png)

In this implementation, we have used the custom typeof operator, to check whether it is an array or an object or a date, or something else. This function will do only shallow comparisons at one level, not on the nested level. Let's see an example:

![img8](https://cdn-images-1.medium.com/max/1024/1*DUi8i9y51cskVgQx8iz3kQ.png)
_Examples_

### 2. deepCompare()

Deep comparison is a type of comparison which checks if two values are equal it should be checked on all levels which means nested object references are ready for comparison. Deep comparison is tested on nested objects, arrays, etc.

#### **Custom Implementation — deepCompare()**

![img9](https://cdn-images-1.medium.com/max/896/1*KWa118ualbzLe8FH8lVU8Q.png)
_Deep comparison_

There is only one point to note is that in deep comparison, you need to do a recursive call to the same function due to comparison at the multi-level or nested level. As we have nested objects, and arrays, we need to check them on multiple levels.

And also, due to recursive call, a deep comparison is comparatively slow to a shallow comparison.

Let’s see the examples:

![img10](https://cdn-images-1.medium.com/max/1024/1*C4Vd_uB1MMNODH99Toykig.png)
_Deep comparison examples._

So, we have learnt about the custom implementation of typeof operator, shallowCompare(), and deepCompare() functions in JavaScript and how they are being used to work and compare between primitive and non-primitive data types.

In the coming parts of the custom implementation, we will see more custom implementation which will help you understand what is happening behind the scenes. Stay tuned for more!

_Hope you liked it, please clap, subscribe and share and also save it for future reference. :)_

_Thanks_
* * *