JavaScript数组unshift()
方法将一个或多个元素添加到数组的开头,并返回该数组的新长度。
您可以尝试运行以下代码,以了解如何在数组的开头添加新的数组元素:
<html> <head> <title>JavaScript Array unshift Method</title> </head> <body> <script> var arr = new Array("orange", "mango", "banana", "sugar"); var length = arr.unshift("water"); document.write("Returned array is : " + arr ); document.write("<br /> Length of the array is : " + length ); </script> </body> </html>
输出结果
Returned array is : water,orange,mango,banana,sugar Length of the array is : 5