


Access JavaScript property case-insensitively
You can alternatively already provide the searchKey in lowercase. If you want it as a function: If the key can’t be found, then it’ll return undefined, just like normal. If you […]

Rename keys in an array of objects using JavaScript
I have an array of country objects. In this array I would like to replace the key name with country. We can use map function to manipulate the array easily.

Generate random string with only numbers or alphabet in JavaScript
This is a simple approach to generate random string with either numbers or letters using a predefined set.

How to change the page title with JavaScript?
To change the page title with JavaScript, we can set the document.title property. This can be useful if you want to change the title of the page to the current context of […]

TypeError[ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension “.json” – How to fix ?
Since ES6 when you try to import JSON file you might face this error. To fix this error simple add the flag –experimental-json-modules to your node.js run command.

Filter on all fields in an array of objects
We know how to filter on an array of objects on specific fields. In some cases we might want to filter on all fields. This code snippet would help.

Get all dates within range in react js
function (startDate, endDate, addFn, interval) { addFn = addFn || Date.prototype.addDays; interval = interval || 1; var retVal = []; var current = new Date(startDate); while (current <= endDate) { retVal.push(new Date(current)); current = addFn.call(current, interval); } return retVal; }

Round a decimal number to nearest decimal that is divisible by 5 in JavaScript
I would like to convert 1.2 to 1.5 and 1.7 to 2. This is basically rounding a decimal number to nearest decimal that is divisible by 5.

Error: algorithms should be set – JWT Error – How to fix?
To fix the error – Error: algorithms should be set – just add the algorithms to the JWT constructor.

Calculate moving average of an array of numbers in JavaScript
Moving average is the average of the current element and the previous elements in an array. We can use the following code snippet to achieve it.

Check if a variable is a date in JavaScript?
We can use these code snippets to determine if a variable is a date in JavaScript. instanceOf Object.prototype.toString.call()

How to filter an object by key in JavaScript?
We can use these code snippets to filter a JavaScript object their keys. Filter and reduce with Object.keys() Filter with Object.entries()

Convert RGB to Hex in JavaScript
Convert RGB to Hex in JavaScript with the following code snippet.

Find the day of year from date in JavaScript
Find the day of year from date in JavaScript with the following code snippet.

Find the number of days between two days using JavaScript
Find the number of days between two days using JavaScript with this code snippet.

Shuffle an array in random order with JavaScript
Shuffle an array in random order with JavaScript with this code snippet.

Get selected text in JavaScript
In JavaScript we can get the text the user has selected using getSelection property

Detect dark mode in JavaScript
Check if a user’s device is in dark mode with the following code snippet.

Truncate a string at a given length using JavaScript
This code snippet truncates or cuts off a text string after a certain number of characters and add an ellipsis (...) at the end. Source: Coderrocketfuel

Compare JSON objects ignoring the order of the properties in JavaScript
To compare two JSON objects we cannot use equality operators === (in case of JavaScript), == (in case of Java). In JavaScript we primarily compare two JSON objects by converting […]

What are the possible ways to create objects in JavaScript?
There are many ways to create objects in JavaScript as described below. Object constructor The simplest way to create an empty object is using the Object constructor. This approach is […]

Inserting and removing elements at a particular position from a JavaScript array
Let’s define an array in JavaScript. Let’s say I would like to insert an element at the fourth position in this array. To remove an element at an index we […]

How to get a key in a JavaScript object by its value?
Let’s take a JavaScript object. To find the key of value ‘B’,

Color your text in console using JavaScript
To colorize your console text we add a few special characters to your text that correspond to a color code. For background colors we need to add a few special characters […]

How to get client’s IP address using JavaScript?
There’s no built-in solution in JavaScript to get IP Address of the client’s server but there are other cool ways. Using public APIs that returns IP Address is one of […]

Defining variables inside the If statement in JavaScript
If statement is used validate an expression to boolean value, like checking a primitive/constant value against a JavaScript variable, but sometimes we would have wrongly assigned a value to a variable […]

Get total number of links from a HTML document using JavaScript
All links in a HTML document is stored in document.links as a HTMLCollection. You can get the total number of links in a HTML web page like: Similar to PluginArray […]

How to represent negative infinity in JavaScript?
Negative infinity in JavaScript is a constant value which is used to represent that no other number is lesser than this value. Negative Infinity can be generated using a self-made […]

Pad a number with leading zeros in JavaScript
In JavaScript, to pad a number with leading zeros, we can use the padStart() method. The padStart() method pads the current string with another string until the resulting string reaches the given […]

Get all method names of an Object in JavaScript
The Object.getOwnPropertyNames() method returns a string array of all properties implemented directly upon a given object. We can use this method to return all methods in a given object. Syntax Example

How to remove property from an array of objects in JavaScript?
In this short post, we are going to discuss how to remove property from an array of objects in JavaScript. Removing fields from an array of objects is necessary for […]

Add, remove and update elements in a JSON tree using JavaScript
Working with Tree structure is a norm these days. In JavaScript, it’s easy to work with JSON but not so with complex tree structures. But it’s always good to go […]

Swap two array elements in JavaScript
In this short post, we are going to see about how to swap two array elements in JavaScript. Here, I have listed some methods that I’ve learnt to swap two […]

JavaScript String.prototype.replaceAll
In JavaScript, we can’t replace the all occurance of substring in a string without use of global regex. In our previous post that is Replace all instances of a string […]

Deep Cloning Objects In JavaScript
In this article, we are going to discuss deep cloning objects in JavaScript. In JavaScript, Object play a major role in storing and sharing data. Because objects in JavaScript are […]

JavaScript String method toLowerCase() and toUpperCase()
JavaScript provides two helpful functions for converting text to uppercase and lowercase. String.toLowerCase() converts a string to lowercase String.toUpperCase() converts a string to uppercase. Both methods works in all modern browsers.

Numeric separators in JavaScript
In this post we will learn about the new feature in JavaScript called Numeric Separators which will be introduced in ES2021. Numeric separators enables developers to make their numeric literals […]

Comments in JavaScript
Let’s take a quick look at the JavaScript comments, the following are some of important points about the comments. Comments in JavaScript are used to explain the code and make the program more readable for […]

How to check if variable is an Array in JavaScript
So you want to check whether a variable is an Array or not. Here ECMAScript 5 gives you a hand 👏. Using Array.isArray() method we can achieve this. Below is […]

Split an array into half in JavaScript
Do you want to split an array into half in JavaScript, divide exactly into equal part? You can use the Array.prototype.splice() method When the length of the array is in […]

Breaking the Loop in JavaScript
In this post, we will see how to break out from a loop in JavaScript. When we work with array or an object sometime we need to iterate the each […]

How to return multiple values from a JavaScript function?
Learn how to return multiple values from a JavaScript function. No, I don’t use an Array to return multiple values. Using an array to hold many values, return the array […]

How to find type of variable in JavaScript?
In this tutorial, let’s learn how to identify the type of a variable in JavaScript. In short, the answer in typeof. In JavaScript, the typeof operator returns the data type of its operand in […]

Create a HTML tt element from a JavaScript string
This JavaScript tutorial explains how to create a HTML <tt> tag using the string method called fixed() with syntax and examples. In JavaScript, fixed() is a string method that is […]

split() String Method in JavaScript
In this short post we will see the use cases of split() method in JavaScript. The split() method separates an original string into an array of sub-strings, based on a separator string that you […]

Array.find Method in JavaScript
In this post let’s take a look at how the Array.find() method works with some easy examples. Array.find() is a JavaScript method that can searches through the array or an […]

How to accept infinite parameters in a JavaScript function?
In this quick post, let’s take a look at how to accept infinite arguments inside a JavaScript function. We can extend the spread operator (…) in JavaScript to accept unlimited […]

Converting an Array of Objects to an Object
In this post we will see the different ways to converting an Array to an Object. Below list are some of the ways that I have encountered during my programming […]

How to convert JavaScript Array to comma separated string?
In this small post, let’s take a look at how to convert an array of elements to a CSV string in JavaScript. You can use Array.prototype.join() method to join the […]

Trim String in JavaScript
Today we are going to see how to smash the whitespaces from the String in JavaScript. First let’s see how the whitespaces are created, Space Tab Line terminator character (used […]

Different ways to check if an Object is empty using JavaScript
Today we are going to see the different methods to find the object that we using is empty or not. This is not that much easy to check the object […]

String match in JavaScript
Today we are quickly going to look out how the String match() works. This method makes our string contains check easy. Syntax: It allows us to do the creative way […]

How to randomly shuffle elements in a JavaScript array?
In this post, we will learn how to randomly shuffle elements in a JavaScript array using various method in JavaScript. Pure JavaScript This shuffling method is implemented using Fisher-Yates algorithm […]

Convert an Object to an Array in JavaScript
In this post, we will see how to convert an object to an array in JavaScript. To achieve this we can use below three methods. Object.keys() Object.values() Object.entries() Let’s go […]

Merging Objects Without Duplicates in JavaScript
In this article, we will see how to merge more complex objects or arrays based on unique values on two different objects. Let’s learn that technique together. Merging two or […]

How to filter an array with another array?
In this article, I would like to share with you some of the ways to filter array with another array. Below are some methods that I have learnt. filter() forEach() […]

How to get keys from JavaScript Object?
In this post, we will see how to get the key set from the object in JavaScript. Below are some basic methods which I have learnt in my professional software […]