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

How to create an array with variable number in js

醉折花枝作酒筹
Release: 2021-08-12 10:45:59
Original
3050 people have browsed it

In the previous article, we learned how to use includes() to determine whether an array contains a specified value. Please see "How to use includes() in js to determine whether an array contains a specified value". This time we will take a look at how to create an array with a variable number. You can refer to it if you need it.

Do you still remember that the editor introduced How to create an array object? If you don’t remember or don’t know, you can click on the text to view it.

Previously we introduced how to create an array object through array. Today we are going to create a variable-length array, so we can no longer use array to create it. So what do we need to use to create it?

First let’s look at a small example.

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

This is the array object created through array before. The result is

How to create an array with variable number in js

Let’s take a look at the array created through array.of.

var nums=new Array.of(7);
console.log(nums);
Copy after login

The result of this is

How to create an array with variable number in js

You can see the difference right now.

Although they are both 7, the 7 in array represents the length of the array, while the 7 in Array.of represents creating an array with a single element 7.

Having said so much, let us take a look at this method in detail.

The Array.of() method creates a new array instance with a variable number of arguments, regardless of the number or type of arguments. The difference between Array.of() and Array constructors is the handling of integer arguments: Array.of(7) creates an array with a single element 7 , while Array(7) creates an empty array of length 7 (note: this refers to an array with 7 empty positions (empty), not an array composed of 7 undefined).

Let’s take a look at the syntax format of this method.

Array.of(任意个参数)
Copy after login

The parameters of this method can be any number, and these parameters will become the elements in the returned array in order.

That’s all. If you need it, you can read: javascript advanced tutorial

The above is the detailed content of How to create an array with variable number 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!