How to Insert an Element into an Array in javaScript?
When “How to Insert an Element into an Array in javaScript?” moves through design, development and browser testing, more details are available here can clarify hand-offs and time spent on revisions while leaving code quality, accessibility and released behaviour as the real measures of success.
For API changes, browser support and current usage patterns, compare the snippet with MDN Web Docs before adopting it in production.
This article is focused on javascript push() array – add element. I explained simply about array into array. We will use how to add to an array with the push. you will learn unshift. Let's get started with and concat functions.
There are several ways to insert an element into an array in JavaScript:
Example 1: Using the push() method: This method adds one or more elements to the end of an array.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How to Insert an Element into an Array in javaScript? - NiceSnippets.Com</title>
</head>
<body>
</body>
<script type="text/javascript">
let myArray = [1, 2, 3];
myArray.push(4);
console.log(myArray); // Output: [1, 2, 3, 4]
</script>
</html>
Example 2: Using the unshift() method: This method adds one or more elements to the beginning of an array.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How to Insert an Element into an Array in javaScript? - NiceSnippets.Com</title>
</head>
<body>
</body>
<script type="text/javascript">
let myArray = [1, 2, 3];
myArray.unshift(0);
console.log(myArray); // Output: [0, 1, 2, 3]
</script>
</html>
Example 3: Using the splice() method: This method can be used to add or remove elements from an array at a specified index.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>How to Insert an Element into an Array in javaScript? - NiceSnippets.Com</title>
</head>
<body>
</body>
<script type="text/javascript">
let myArray = [1, 2, 3];
myArray.splice(1, 0, 'a', 'b');
console.log(myArray); // Output: [1, 'a', 'b', 2, 3]
</script>
</html>
In this example, we are adding two elements ('a' and 'b') at index position 1 (after the first element).
Note: The first argument of splice() is the index where you want to start adding or removing elements. The second argument is the number of elements you want to remove (in this case we don't want to remove any). The third argument and onwards are the elements you want to add.
- How to set Date in setUTCHours() Method in JavaScript?
- How to Date sets UTC milliseconds in Javascript?
- How to Use push(), unshift(),pop() and shift() With Array in Javascript?
- How to Check if a Variable is a Number in Javascript?
- How To Format a Number Using Fixed - Point Notation in Javascript?
- How to remove duplicate elements from JavaScript Array?
- How to calculate years, months and days between two dates in javascript?
- How to check for IP address using regular expression in Javascript?