Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to create a JavaScript array

Shulou Source: shulou.com Published: 2022-05-31 11:44:51 09月27日 Update

This article mainly introduces "how to create a JavaScript array". In daily operation, I believe many people have doubts about how to create a JavaScript array. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to create a JavaScript array". Next, please follow the editor to study!

Method: 1, use the new operator to call Array () type function to construct a new array, syntax "var a = new Array ();"; 2, use "[]" array direct quantity, syntax "var a = [values list];", the list can be empty, or it can be a multi-valued list, with values separated by commas.

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

How JavaScript defines / creates arrays

JavaScript defines (creates or declares) an array in two ways: constructing an array and an array literal.

Construct array

When you use the new operator to call a function of type Array (), you can construct a new array.

Example 1

You can create an empty array by directly calling the Array () function without passing arguments.

Var a = new Array (); / / empty array

Example 2

Pass multiple values to create a real array.

Var a = new Array (1, true, "string", [1Mague 2], {XRO 1je YJR 2}); / / Real array

Each parameter specifies the value of an element, and there is no limit to the value type. The order of parameters is also the order of array elements, and the value of the length attribute of the array is equal to the number of parameters passed.

Example 3

Pass a numeric parameter that defines the length of the array, that is, the number of elements contained.

Var a = new Array (5); / / an array of specified length

The parameter value is equal to the attribute value of the array length, and the default value for each element is undefined.

Example 4

If you pass a parameter with a value of 1, JavaScript defines an array of length 1 instead of an array containing an element with a value of 1.

Var a = new Array (1); console.log (a [0])

Array direct quantity

Syntax format for array literals: contains multiple lists of values in square brackets, separated by commas.

Example

The following code uses array literals to define an array.

Var a = []; / / empty array var a = [1, true, "0", [1Magi 0], {XRX 1djj0}]; / / an array containing specific elements

It is recommended to use array direct quantities to define arrays, because array direct quantities are the easiest and most efficient way to define arrays.

At this point, the study on "how to create a JavaScript array" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Arrays parameters elements examples methods learning functions types syntax length number between multiple attributes more operators commas order help operations Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Microsoft Docker OPPO Reno Linux