Get the App
SLTechnology News&Howtos  ›  Development  › 

How to define arrays in javascript

Shulou Source: shulou.com Published: 2022-06-03 05:06:05 09月22日 Update

This article will explain in detail how to define arrays in javascript. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The method of defining array in javascript: 1, use "var array name = [values list]" statement; 2, use "var array name = new Array (values list)" statement; 3, use "var array name = new Array (array length value); array name [subscript] = value;" statement.

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

Define using array direct quantities

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

Var array name = [values list]

If the values list can be empty, an empty array is created.

Example:

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

Define by using the constructor

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

Var array name = new Array (); / / empty array var array name = new Array (values list); / / real array

If you pass a numeric parameter to Array (), the parameter value is equal to the attribute value of the array length, and you can define the length of the array, that is, the number of elements contained.

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

At this point, the default value of each element is undefined, which can be initialized by assigning a value to each element:

This is the end of the article on "how to define arrays in javascript". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Arrays elements methods articles statements length functions parameters more good practical subscript number between creative content can pass multiple attributes tutorials Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Huawei vpn Redmi Docker