Home > Web Front-end > JS Tutorial > body text

How to create an array object in js

醉折花枝作酒筹
Release: 2021-08-09 17:26:01
Original
5017 people have browsed it

This article will start a new chapter. Everything we want to perform cannot be separated from arrays. Today we will learn the simplest one, which is to create an array object. Only after it is created can it be used. Come and learn.

What we want to learn is arrays, so how can we not know what arrays are? Let’s first take a look at what an array is.

An array is an ordered sequence of elements. If you name a collection of a limited number of variables of the same type, this name is the array name. The variables that make up an array are called components of the array, also called elements of the array, and sometimes called subscript variables. The number used to distinguish the elements of an array is called a subscript. An array is a form in which multiple elements of the same type are organized in an ordered form for ease of programming. A collection of these homogeneous ordered data elements is called an array.

An array is a collection used to store multiple data of the same type.

After knowing these basic knowledge, let's look at how to create an array object.

var num = ['one', 'two'];
console.log(num.length);
Copy after login

Look at the results of this.

How to create an array object in js

#As you can see, we have created two array objects. Let’s look at another example.

var nums = new Array("one","two","three");
console.log(nums.length);
Copy after login

The result is

How to create an array object in js

In this way we have created three array objects, let's look at an example again.

var arr = new Array(3); 
arr[0] = "one";
arr[1] = "two";
arr[2] = "three";
console.log(arr.length);
Copy after login

The result is

How to create an array object in js

We looked at three examples and used three methods to create array objects. Do you guys see the pattern?

Then let me talk about it.

方法一:
new Array();
方法二:
new Array(期望的数组元素个数);
方法三:
new Array(参数列表1,参数列表2, ..., 参数列表n);
Copy after login

Returns the newly created and initialized array. If the constructor array() is called without arguments, the returned array is empty and the length field is 0. When the constructor is called, passing it only a numeric argument, the constructor returns an array containing the specified number of elements and undefined elements.

When array() is called with other parameters, the constructor initializes the array with the value specified by the parameter. When a constructor is called as a function without the new operator, it behaves exactly as when called with the new operator.

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of How to create an array object in js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!