Home >Web Front-end >JS Tutorial >How to use the unshift method
The unshift() method can add one or more elements to the beginning of the array, increase the length of the existing array by adding the number of elements to the array and return the new length. Let's take a look at the specific use of unshift() method.
Let’s first take a look at the basic syntax of unshift()
arr.unshift(element1[, ...[, elementN]])
element represents the added element, the number of parameters and the requirements The same number of elements are added at the beginning of the array.
Let’s look at a specific example
The code is as follows
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script> function sayHello() { var arr = [23,76,19,94]; document.write(arr.unshift(28,65)); document.write("<br>"); document.write(arr); } sayHello(); </script> </body> </html>
The running results are as follows: 6 is the length of the new array returned, and the following are the elements of the new array.
6 28,65,23,76,19,94
This article ends here. For more exciting content, you can pay attention to other related column tutorials on the php Chinese website! ! !
The above is the detailed content of How to use the unshift method. For more information, please follow other related articles on the PHP Chinese website!