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

What are the methods of defining arrays in javascript

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about javascript array definition methods have what content. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

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

Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.

defined using array literals

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

var Array Name =[Value List]

The list of values can be empty, an empty array is created.

Examples:

var a = []; //Empty array var a = [1, true, "0", [1,0], {x:1,y:0}]; //Array containing concrete elements

Definition using constructor

When you call an Array() type function with the new operator, you construct a new array.

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

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

var a = new Array(5); //Array of specified length

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

A[0] = 1;A[1] = 2;A[2] = 3; Thank you for reading! About "javascript array definition methods have what" this article is shared here, I hope the above content can be of some help to everyone, so that we can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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