Get the App
SLTechnology News&Howtos  ›  Development  › 

How JavaScript uses separate variable names to store multiple values

Shulou Source: shulou.com Published: 2022-06-01 13:12:04 10月04日 Update

This article mainly introduces how JavaScript uses separate variable names to store multiple values, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

JavaScript Array object

The Array (array) object is used to store a series of values using separate variable names.

Define array

Array objects are used to store a series of values in separate variable names.

Use the keyword new to create array objects. The following code defines an array object named myArray:

Var myArray=new Array ()

By assigning values to an array, you can add as many values as you want, just as you can define as many variables as you need.

There are two ways to assign values to an array:

1 、

Var mycars=new Array () mycars [0] = "Saab" mycars [1] = "Volvo" mycars [2] = "BMW"

You can also use an integer argument to control the capacity of the array:

Var mycars=new Array (3) mycars [0] = "Saab" mycars [1] = "Volvo" mycars [2] = "BMW"

2 、

Var mycars=new Array ("Saab", "Volvo", "BMW")

When specifying numeric or logical values in an array, the variable type should be a numeric variable or a Boolean variable, not a character variable.

Access array

You can access a particular element by specifying the array name and index number. For example:

[xss_clean] (mycars [0])

Note: the above line of code will output Saab.

Modify values in an existing array

To modify a value in an existing array, simply add a new value to the specified subscript:

Mycars [0] = "Opel"; [xss_clean] (mycars [0])

Note: at this point, the above line of code will output Opel.

Create an array

Create an array, assign values to it, and then output these values:

Var mycars = new Array () mycars [0] = "Saab" mycars [1] = "Volvo" mycars [2] = "BMW" for

Tags: Array element object variable method comment character store string more article output two code numeric result faceted sort multiple attribute Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei macOS MariaDB Docker