Adding and Removing Items from an Array. Javascript

Ali Toshmatov
2 min readMar 15, 2021

--

Arrays essential part in most programming languages including javascript. Mastering it makes your programming life easier than you think. This article is for javascript beginners, and it provides essential array methods every programmer should know.

Unlike some other programming languages like java, c++, c,… in javascript array size is not fixed, meaning that you can add, remove, and change array size however you want.

Adding new item

push() method is used to add new items to the end of an array. You can pass items as an argument of this method.

let arr = [1,2,3];
arr.push(10, 11);
arr.push(100);
console.log(arr); //[1,2,3,10,11,100]

unshift() method is used to add new items from the beginning of the array. Here also you should pass new items as an argument of the method.

let arr = [1,2,3];
arr.unshift(10, 11);
arr.unshift(100);
console.log(arr); //[100, 10, 11, 1, 2, 3]

Removing items

pop() method is used to remove the last item of an array.

let arr = [1,2,3];
arr.pop();
console.log(arr); //[1,2]

shift() method is used to remove the first item of an array. After removal of the first item all items are shifted forward and their index change respectively.

let arr = [1,2,3];
arr.shift();
console.log(arr); //[2,3]

Note: Keep in mind that these methods directly change the current array and they return different results.

Inserting or removing items in the middle

splice() method is used for both inserting and removing items in the middle of an array. This method takes 2 main arguments. This method removes a specified number of items starting from a specified index. The first argument is the index of an array where removal should start. The second argument is the number of items to be removed.

let arr = ["a", "b", "c", "d", "e"];
arr.splice(1,2);
console.log(arr); //["a", "d", "e"]

After the second argument, you can pass as many values as you want in order to insert them in the same place.

let arr = ["a", "b", "c", "d", "e"];
arr.splice(1,2, "x", "y", "z");
console.log(arr); //["a", "x", "y", "z", "d", "e"]

Other methods

This one is not a method and guess you are very familiar with it but thought it worth mentioning this. length every array has this property and it keeps track of the length of our array.

const arr = [1,2,3,4,5];
console.log(arr.length); //5

slice() method is used to take some part of an array. The index of starting point is passed as the first argument, and the ending index is passed for the second argument. The ending index will not be included in the extracted array. If there is no second argument it extracts items till the end of an array. Keep in mind that this method returns extracted array and won’t change the original array.

let arr = ["a", "b", "c", "d", "e"];
const arr1 = arr.slice(1, 3);
console.log(arr1); //["b", "c"]
const arr2 = arr.slice(2);
console.log(arr2); //[ "c", "d", "e"]

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response