What is the use of the Push method in JavaScript?
Answers (1)
Add AnswerBelow is an example of the Array push() method.
<script> function func() { var arr = ['GFG', 'gfg', 'g4g']; // Pushing the element into the array arr.push('abc'); document.write(arr); } func(); </script>
Output:
GFG,gfg,g4g,abc
The arr.push() method is used to push one or more values into the array. This method changes the length of the array by the number of elements added to the array.
Syntax:
arr.push(element1, elements2 ....., elementN]])
Parameters This method contains as many numbers of parameters as the number of elements to be inserted into the array.
Return value: This method returns the new length of the array after inserting the arguments into the array.
The below example illustrate the JavaScript Array push() method:
Example 1: In this example, the function push() adds the numbers to the end of the array.
var arr = [34, 234, 567, 4]; print(arr.push(23,45,56)); print(arr);
Output:
7 34,234,567,4,23,45,56
Supported Browsers: The browsers supported by the JavaScript Array push() method are listed below:
- Google Chrome 1.0 and above
- Microsoft Edge 12 and above
- Mozilla Firefox 1.0 and above
- Safari 1 and above
- Opera 4 and above
The push () method is used to add elements to an array.
For Example,