Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to define an array in javascript

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to define the array in javascript. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

There are arrays in javascript. In javascript, an array is a collection of data arranged in order, each member of the array is called an element, and the name (key) of each element is called the array subscript (Index); the length of the array is flexible and readable.

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

Arrays in javascript

In javascript, an Array is a collection of ordered data, each member of the array is called an element (Element), and the name (key) of each element is called an array subscript (Index).

The length of the array is flexible and readable (using the length attribute to read and write).

How to define an array

Method 1: use square brackets [], and each element in the array is separated by a comma

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

Method 2: call the Array () function

Var a = new Array (); / / empty array var a = new Array (1, true, "string", [1JI 2], {XRX 1L YRO 2}); / / Real array

Note: if the Array () function passes a numeric parameter, 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

The elements in the array can be accessed by index. The index in the array starts at 0 and increases in turn, that is, the index of the first element of the array is 0, the index of the second element is 1, the index of the third element is 2, and so on. As shown in the following example:

Var arr = [1,2,3.14, 'Hello', null, true]; console.log (arr [0]); / / output the element with index 0, that is, 1console.log (arr [5]); / / output the element with index 5, that is, trueconsole.log (arr [6]); / / the index is out of range, and return all the contents of the article "how arrays in javascript are defined" above undefined. Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report