Use These JavaScript Methods to Add and Remove Items from an Array

Use These JavaScript Methods to Add and Remove Items from an Array

Use These JavaScript Methods to Add and Remove Items from an Array. The method push() takes one argument, which is the element we want to add at the end of an array. In this article, we will discover some JavaScript methods to add and remove items from an array.

Add and remove items from an array in JavaScript.

JavaScript is a great and easy-to-use programming language in my opinion. You can create almost anything with it such as websites, mobile apps, games, AI, and so on. In addition to that, it has a lot of useful methods that we can use to work with different data types, such as arrays for example.

In this article, we will discover some JavaScript methods to add and remove items from an array. So let’s get started.

The push method

I think every JavaScript beginner knows about the method push() . It basically allows us to add an item or an element to the end of an array.

The method push() takes one argument, which is the element we want to add at the end of an array.

Here is an example:

let colors = ["red", "black", "green"];
colors.push("yellow");

console.log(colors); //["red", "black", "green", "yellow"]

As you can see, the yellow color gets added to the end of the array. You can add any type of data(numbers, string, objects…).

The method unshift

The method unshift() is an array method that allows us to add items or elements at the beginning of an array.

Here is another colors example using the method unshift() :

let colors = ["red", "black", "green"];
colors.unshift("yellow");

console.log(colors); //["yellow", "red", "black", "green"]

So in the example above, the yellow color gets added to the beginning of the array. As you can see, the method unshift() takes one single argument(the element that we want to add).

programming coding javascript

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript

4 Ways You Can Get Rid of Dirty Side Effects for Cleaner Code in JavaScript. Bugs are born in many ways. Creating side effects is one of them. Some people say side effects are evil, some say they’re not.

Who Else Wants to Write Clean JavaScript Code?

Who Else Wants to Write Clean JavaScript Code? 7 Tips to Make Your Coworkers Fall in Love With Your Code.

Code Formatting with Prettier in Visual Studio Code

Formatting code consistently is a pain, especially when working on a team. The beauty of modern-day web development is that the tooling has gotten so much better! In this article, we will look at setting up Prettier to automatically format your code in Visual Studio Code.

Top 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript

Don’t waste time writing long code while you can make it short, yet clearer and easier to read. In this Javascript tutorial, we'll discuss 15 Simple Coding Techniques to Get Your Tasks Done with Shorter Code in JavaScript