
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
Adding an element in an array using Javascript
Adding an element to an array can be done using different functions for different positions.
Adding an element at the end of the array
This can be accomplished using the push method. For example,
let veggies = ["Onion", "Raddish"]; veggies.push("Cabbage"); console.log(veggies);
This will give the output −
["Onion", "Raddish", "Cabbage"]
You can also use this to push multiple items at the same time as it supports a variable number of
arguments. For example,
let veggies = ["Onion", "Raddish"]; veggies.push("Cabbage", "Carrot", "Broccoli"); console.log(veggies);
This will give the output −
["Onion", "Raddish", "Cabbage", "Carrot", "Broccoli"]
Adding an element at the start of the array
This can be accomplished using the unshift method. For example,
let veggies = ["Onion", "Raddish"]; veggies.unshift("Cabbage"); console.log(veggies);
This will give the output −
["Cabbage", "Onion", "Raddish"]
You can also use this to unshift multiple items at the same time as it supports a variable number of
arguments. For example,
let veggies = ["Onion", "Raddish"]; veggies.unshift("Cabbage", "Carrot", "Broccoli"); console.log(veggies);
This will give the output −
["Cabbage", "Carrot", "Broccoli", "Onion", "Raddish"]
Adding an element at a given position of the array
Sometimes you need to add an element to a given position in an array. JavaScript doesn't support it out of the box. So we need to create a function to be able to do that. We can add it to the Array prototype so that we can use it directly on the object.
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(-1, 2); console.log(arr);
This will give the output −
[1, 2, -1, 3, 4]
Now the insert method is available on every array object you create.
You can also use the splice method to insert elements at given positions. For example,
var months = ['Jan', 'March', 'April', 'June']; months.splice(1, 0, 'Feb'); console.log(months);
This will give the output:
['Jan', 'Feb', 'March', 'April', 'June']
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
- Adding an element at a given position of the array in Javascript
- Removing an element from an Array in Javascript
- Searching an element in Javascript Array
- How to insert an element into all positions in an array using recursion - JavaScript?
- Convert given array to Arithmetic Progression by adding an element in C++
- Find the middle element of an array using recursion JavaScript
- Iterating through an array, adding occurrences of a true in JavaScript
- Adding two values at a time from an array - JavaScript
- Adding an element into the Hashtable in C#
- Adding an element to the List in C#
- How to validate if an element in an array is repeated? - JavaScript
- Inserting element at falsy index in an array - JavaScript
- Finding the first redundant element in an array - JavaScript
