Some cool and awesome JavaScript tricks

August 05, 2019

In this article, we will cover some of the cool and awesome JavaScript tricks which you may find useful at some point while coding in JavaScript.




Trick 1

Identifying if a variable is of primitive or non-primitive data type

There are primitive and non-primitive data types in Javascript. The primitive types include boolean, string, number, BigInt, null, Symbol and undefined. The non-primitive types include the Objects.
var val; // contains either primitive or non-primitive value
function isPrimitive(val) {
    return Object(val) !== val;
}

Trick 2

Creating a pure object in Javascript

Before creating a pure object, let’s understand “What is a pure object?”
var obj = {};
var obj = Object.create(null);


Trick 3

Removing duplicates from an array

Consider the following array
var arr = [1, 2, 3, 3, 4, 5, 6, 6, 7, 7, 7, 7];
const newArr = [...new Set(arr)];



You Might Also Like

0 comments

Follow by Email