Home >Web Front-end >JS Tutorial >How to use the unshift method

How to use the unshift method

不言
不言Original
2019-02-16 11:40:124869browse

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.

How to use the 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn