Home  >  Article  >  Web Front-end  >  How to add elements to an array in JavaScript? 3 ways to add elements to js arrays (code examples)

How to add elements to an array in JavaScript? 3 ways to add elements to js arrays (code examples)

青灯夜游
青灯夜游Original
2018-10-22 11:08:0352268browse

Arrays are an important part of JavaScript. When learning js arrays, the operation of array elements is an indispensable part. So do you know how to add array elements? This article will introduce to you how to add elements to a js array (one-dimensional), so that everyone can understand the method of adding elements to a js array. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

First of all, let’s briefly introduceWhat are the three methods of adding elements to js arrays? They are:

1, js push() method to add array elements

2, js unshift() method to add array elements

3, js splice( ) method to add array elements

Let’s introduce the above method in detail How to add elements to js array, through a simple code example.

js push() method adds array elements

push() method can add one or more new elements to the end of the array, The length of the new array is then returned, and the push() method is supported by all major browsers.

Syntax:

数组.push(元素1,元素2,元素3.....元素n);/*push()方法里必须有一个参数*/

Code example: Add the two elements dog1 and dog2 to the end of the animal array



	
		
	
	
		

数组:cat,elephant,tiger,rabbit;
数组长度为:4

Effect picture:

How to add elements to an array in JavaScript? 3 ways to add elements to js arrays (code examples)

Description:

Array.length can return the length of the array

js unshift() method adds array elements

The unshift() method can add one or more new elements to the beginning of the array and then return the length of the new array, and all major browsers support the unshift method.

Grammar:

数组.unshift(元素1,元素2,元素3.....元素n);/* unshift()方法里必须有一个参数*/

Code example: Add the two elements dog1 and dog2 to the beginning of the animal array



	
		
	
	
		

数组:cat,elephant,tiger,rabbit;
数组长度为:4

Effect picture:

How to add elements to an array in JavaScript? 3 ways to add elements to js arrays (code examples)

js splice() method adds array elements

The splice() method can add one or more new elements to the array At the specified position, the element at the inserted position is automatically moved back, and all major browsers support the splice method.

Grammar:

数组.splice(index,howmany,item1,.....,itemN);

index: Indicates where to add or delete elements;

howmany: Indicates how many elements should be deleted. A value of 0 means no elements will be deleted;

item: Represents a new element to be added to the array.

Code example: Add the two elements dog1 and dog2 to the second position of the animal array (after the first element)



	
		
	
	
		

数组:cat,elephant,tiger,rabbit;
数组长度为:4

Rendering:

How to add elements to an array in JavaScript? 3 ways to add elements to js arrays (code examples)

The above are the three methods of adding elements to js arrays introduced in this article, namely push() method, unshift() method and splice() method. Which method to choose at work depends on work needs and personal habits. Newbies can try it themselves to deepen their understanding. I hope this article can help you! For more related tutorials, please visit: JavaScript Video Tutorial, jQuery Video Tutorial, bootstrap Video Tutorial!

The above is the detailed content of How to add elements to an array in JavaScript? 3 ways to add elements to js arrays (code examples). 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