Different Ways to Check if an Object is Empty in JavaScript

Learn different ways to check if an object is empty Photo by Felix Mooneeram on Unsplash 1. Using Object.keys Object.keys will return an Array, which contains the property names of the object. If the length of the array is 0, then we know that the object is empty. function isEmpty(obj) { return Object.keys(obj).length === 0;} We can …

Learn BigInt in JavaScript

Learn how to use BigInt in JavaScript to store large numbers Photo by Mika Baumeister on Unsplash BigInt is a built-in object that provides a way to store numbers larger than Number.MAX_SAFE_INTEGER. The reason for the need of BigInt is var maxIntNum = 9007199254740991; var largeNumber = maxIntNum + 2; // 9007199254740992 The expected result for the …

Accessing an Object’s Keys, Values, and Entries in JavaScript

Let’s dive into objects Photo by Matt Artz on Unsplash Object.keys(obj) → returns an array of a given object’s property names. Object.values(obj) → returns an array of a given object’s own property values. Object.entries(obj) → returns an array of a given object’s own string-keyed property [key, value] pairs. var user = { name : “John”, profession : …

Redirect, Refresh, and Access the URL of the Current Page in JavaScript

JavaScript’s location object Photo by Javier Allegue Barros on Unsplash We can use the window.location property to access the URL of the current page and modify it. let location = window.location; For redirecting the page we can use: location.href = “new url”; // or we can use location.assign(“new url”); For redirecting without storing in history: location.replace(“new url”); …

Understand the Superpower of Optional Chaining in JavaScript

Learn how to use upcoming JavaScript feature Optional chaining ?. → Optional Chaining operator Image from twitter by Daniel Rosenwasser (@Daniel Rosenwasser). Optional Chaining Optional chaining will eliminate the need for manually checking if a property is available in an object . With optional chaining the checking will be done internally . Example without Optional chaining. function sayHi(user) …

Understanding the Sort Method of Arrays

How to use JavaScript’s sort Photo by Jozsef Hocza on Unsplash The JavaScript array sort() is used to sort array elements. By default, the array will sort in ascending order, but we can change it. This method will change the original array. We can also provide our comparing function to implement custom sorting. var array = …

Five Ways to Loop Through a JavaScript Array

Learn the different methods to loop through arrays in JavaScript Photo by Zan on Unsplash In JavaScript there are many ways to loop through Array. Loops which can break or skip (continue) an iteration: for while do…while for…in 2. Loops which we cannot break or skip an iteration: forEach Tip: To create an array with a …

Different ways to measure performance in JavaScript

Learn different ways to measure the time it takes to execute programs in JavaScript Image by Aron Visuals To find how much time a function, loop, or any operation takes to execute in JavaScript, we can use one of the following tools: Using console.time and console.timeEnd Using the performance.now method Using online performance testing tools 1. …

Understanding The forEach Method for Arrays in JavaScript

Improve your iteration Photo by Mika Baumeister on Unsplash What is forEach? The forEach method executes the callback function against all element of the array. forEach method in JavaScript Syntax array.forEach(callbackFunction, [, thisArg]); // thisArg is optional arguments for callbackFunction callbackFunction(currentValue, index ,array) { //index, array is optional } The forEach method doesn’t return anything. Let’s start …

Three ways to merge arrays in JavaScript

Learn different ways to merge array in JavaScript Image by Makarios Tang 1. Using for loop We can loop through the array source array which need to be pushed , then add elements one by one function push(fromArray, toArray) { for(let i = 0, len = fromArray.length; i < len; i++) { toArray.push(fromArray[i]); } return toArray;} var array1 …

Design a site like this with WordPress.com
Get started