In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
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!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.