Array (Array)
How to define an array:
1. new Array
var arr = new Array()
new Array
It is similar to the array literal, but new Array
if only one parameter is filled in, then the length of the array will be filled.
E.g:
var arr = new Array(5)
At this point, the length of the array is 5,
The original array is an empty value of undefined, which is displayed differently in different browsers, undefined × 5 or empty × 5. ·
Because a parameter fills in the length of the array, you must fill in an integer, otherwise an error will be reported! ! !
2. Array literals
var arr = [1, 2, 3, 4]
This method is more commonly used.
Note that array literals can be written like this:
var i = [,,,]
will return undefined × 3 or empty × 3
And the parameter of new Array will report an error.
Array reading and writing:
It is not easy to report errors during array operations .
For example an array
var i = [1, 2, 3]
Even if the array length is 3, but output i[10] outputs the tenth digit of the array, no error will be reported, and the value is undefined.