Adding an element at a given position of the array in Javascript


In this article, we are going to learn how to add an element at a given position of the array in JavaScript. An array is a special variable, which can hold more than one value.

An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items together. This makes it easier to calculate the position of each element by simply adding an offset to a base value of the memory location of the first element of the array. The base value is index 0 for the first element in the array and the difference between the two indexes is the offset.

Syntax

Following is the syntax for the array data structure in JavaScript.

const array = ["Value1", "Value2", "Value3"];

Arrays allow random access to elements. This makes accessing elements by position faster and easy. Arrays have better cache locality which makes a pretty big difference in performance. Arrays represent multiple data items of the same type using a single name, instead of storing different items in different variables.

We use insert() function to add an element. The function adds the element at the specified position of an array.

Syntax

Following is the syntax for adding an element at a given position of the array in JavaScript.

arr.insert(item, position);

Example

Following is the example program for adding an element at a given position of the array in JavaScript.

<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script> Array.prototype.insert = function(data, position) { if (position >= this.length) { this.push(data) // Put at the end if position is more than total length of array } else if (position <= 0) { this.unshift(data) // Put at the start if position is less than or equal to 0 } else { // Shift all elements to right for (let i = this.length; i >= position; i--) { this[i] = this[i - 1]; } this[position] = data; } } let arr = [1, 2, 3, 4]; arr.insert(-2, 2); document.write(arr); Output </script> </head> </html>

We can add any data to the array.

Example

Following is the example program for adding an element at a given position of the array in JavaScript.

<!DOCTYPE HTML> <html> <head> </head> <script > const items = ["one", "two" , "three", "four"] const insert = (items, index, newItem) => [...items.slice(0, index),newItem,...items.slice(index)] const result = insert(items, 0, 'zero') document.write(result) </script> </head> </html>

Example

You can also use the splice method to insert elements at given positions.

<!DOCTYPE HTML> <html> <head> </head> <script > var months = ['Jan', 'March', 'April', 'June']; months.splice(1, 0, 'Feb'); document.write(months); </script> </head> <body> </body> </html>

The first argument of the method is the index we want to remove elements from or insert elements into. The second argument is the number of elements we want to remove. And the third argument onwards are the values we would like to insert into the array.

Updated on: 21-Nov-2022

392 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements