How to Append an Item to an Array in JavaScript: Explained

Discover the different ways JavaScript provides for appending an item to an array and learn about the recommended approach. Appending a Single Item In order to append a single item to an array, you can make use of the push() method offered by the Array object. Here’s an example: const fruits = ['banana', 'pear', 'apple']; fruits.push('mango'); It’s important to note that push() modifies the original array. If you want to create a new array instead of modifying the original one, you can employ the concat() method of the Array object....

How to Empty a JavaScript Array

In JavaScript, there are multiple approaches to emptying an array. This blog post will discuss a couple of commonly used methods for clearing an array and emptying all its elements. Method 1: Setting the Array Length The easiest way to empty a JavaScript array is by setting its length to 0. This method works for both const and let declarations. Here’s an example: const list = ['a', 'b', 'c']; list.length = 0; By changing the length of the array to 0, all its elements are automatically removed....

How to Get the Index of an Iteration in a for-of Loop in JavaScript

In JavaScript, a for-of loop is a powerful feature introduced in ES6 that allows you to iterate over an array effortlessly. However, by default, it does not provide a straightforward way to access the index of each iteration. But worry not! In this blog post, I will show you how to easily obtain the index of an iteration using a combination of the destructuring syntax and the entries() method. Let’s dive in!...

How to Split a String into Words in JavaScript

In JavaScript, there is a simple way to split a string into separate words using the split() method. With the split() method, we can define the delimiter, which determines where the string should be divided. Here’s an example of how to use the split() method to cut a string into words when a space is encountered: const text = "Hello World! Hey, hello!"; text.split(" "); The split() method returns an array, and in this case, it will produce an array with four items:...

How to Verify if a JavaScript Array Contains a Specific Value

When working with JavaScript arrays, it is quite common to need to check if a particular item is present. Fortunately, JavaScript provides us with a handy method called includes() that allows us to perform this check easily. To use the includes() method, you simply call it on the array instance, followed by the value you want to check for. The method returns true if the item is found in the array, and false otherwise....

Sorting an Array by Date Value in JavaScript

Learn how to sort an array of items by date value in JavaScript. If you have an array of objects like the one below, which contains a set of date objects: const activities = [ { title: 'Hiking', date: new Date('2019-06-28') }, { title: 'Shopping', date: new Date('2019-06-10') }, { title: 'Trekking', date: new Date('2019-06-22') } ] and you want to sort those activities by the date property, you can use the sort() method of Array....

Understanding the JavaScript `filter()` Function

In JavaScript, the filter() function is an essential method for arrays. It allows you to create a new array by filtering out elements from an existing array based on a specified condition. To use filter(), you simply call it on the array and pass in a function as an argument. This function will be executed for each element in the array, and only the elements that fulfill the condition specified in the function will be included in the new filtered array....

Working with Objects and Arrays Using Rest and Spread

Learn two modern techniques for working with arrays and objects in JavaScript. You can expand an array, object, or string using the spread operator .... Let’s start with an example for arrays: const a = [1, 2, 3]; To create a new array, you can use: const b = [...a, 4, 5, 6]; You can also create a copy of an array using: const c = [...a]; This also works for objects....