2.2 JavaScript Interview Preparation: Array Manipulation
As a beginner in JavaScript, it is essential to learn how to manipulate arrays, which are one of the most commonly used data structures in JavaScript. Manipulating arrays involves adding, removing, inserting, or replacing elements in an array. In this tutorial, we will discuss various methods that can be used to manipulate arrays in JavaScript, including the push, shift and splice methods.
Adding Elements to an Array
Adding elements to an array is a common operation, and the push method is used to add one or more elements to the end of an array. The syntax of the push method is as follows:
array.push(element1, element2, ..., elementN)
where array
is the array to which elements are added, and element1
to elementN
are the elements that need to be added. The push method returns the new length of the array, which is the count of elements after adding the new elements.
For example, let's say we have an array named numbers
that contains the elements 1, 2, 3, and 4
. We can add a new element 5
to the end of the array using the push method as follows:
let numbers = [1, 2, 3, 4];
numbers.push(5);
console.log(numbers);
This will output [1, 2, 3, 4, 5]
to the console.
Similarly, the unshift method can be used to add one or more elements to the beginning of an array. The syntax of the unshift method is as follows:
array.unshift(element1, element2, ..., elementN)
where array
is the array to which elements are added, and element1
to elementN
are the elements that need to be added. The unshift method returns the new length of the array, which is the count of elements after adding the new elements.
Removing Elements from an Array
Removing elements from an array is also a common operation, and the shift method is used to remove the first element from an array. The syntax of the shift method is as follows:
array.shift()
where array
is the array from which the first element needs to be removed. The shift method returns the removed element.
For example, let's say we have an array named numbers
that contains the elements 1, 2, 3, and 4
. We can remove the first element 1
from the array using the shift method as follows:
let numbers = [1, 2, 3, 4];
let removedElement = numbers.shift();
console.log(removedElement);
console.log(numbers);
This will output 1
to the console as the removed element and [2, 3, 4]
as the remaining elements in the array.
Similarly, the pop method can be used to remove the last element from an array. The syntax of the pop method is as follows:
array.pop()
where array
is the array from which the last element needs to be removed. The pop method returns the removed element.
Splice Method: Inserting and Replacing Elements in an Array
The splice method is used to insert, replace, or remove elements from an array. The syntax of the splice method is as follows:
array.splice(startIndex, deleteCount, element1, element2, ..., elementN)
where array
is the array that needs to be manipulated, startIndex
is the index at which elements need to be inserted or deleted, deleteCount
is the number of elements to be deleted (if any), and element1
to elementN
.
The content of this blog post was inspired by the JavaScript - Marathon Interview Questions Series 2023 course on Udemy
Subscribe to my newsletter
Read articles from Anirudha Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Anirudha Patil
Anirudha Patil
Hi, I'm Anirudha ! I'm currently learning frontend development and sharing my journey through blogging. Join me as I document my experiences, struggles, and victories as I become a skilled Frontend Developer.