
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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.
- Related Articles
- Adding an element at the end of the array in Javascript
- Adding an element at the start of the array in Javascript
- Removing an element from a given position of the array in Javascript
- Adding an element in an array using Javascript
- Adding two values at a time from an array - JavaScript
- Convert given array to Arithmetic Progression by adding an element in C++
- Inserting element at falsy index in an array - JavaScript
- Finding the index position of an array inside an array JavaScript
- Insert an element at second position in a C# List
- Iterating through an array, adding occurrences of a true in JavaScript
- How to move an element of an array to a specific position (swap)?
- Find position of an element in a sorted array of infinite numbers in C++
- How to set the top position of an element with JavaScript?
- Finding the majority element of an array JavaScript
- Java program to reverse an array upto a given position
