Basic knowledge of JavaScript arrays

Array


The concept of array

一A collection of group numbers is called an "array".

var arr = [1,2,3,4,5];

var arr = ["Xiao Ming", "Male", 20, "Institute", "Anhui Province" , ];


Array elements

Each value in the array is called an "array element".

Array index

  • There are multiple values ​​in the array, each value has a "number", passed "Number" can access each value in the array.

  • The "number" in the array is also called "subscript" or "index number".

  • The "subscript number" in the array is a positive integer starting from 0. That is to say: the index of the first array element is 0, the index of the second array element is 1, the index of the third array element is 2, and so on.

  • The subscript of the first array element must be 0, and the subscript of the last array element is: length-1.

  • The purpose of using an array is that it is convenient to use a loop to traverse the array.


##Access to array elements

var arr = [10,20,30,40,50];

arr = ["Brother Tao", "Male", 24, "College", "University of Science and Technology Beijing"];

The access method is: the array variable name, followed by a square bracket [], and the [] square bracket is the subscript of the array element. For example: arr[3]

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>
            var arr = ["涛哥" , "男" , 24, "大专" , "北京科技大学" ];
            for(var i=0;i<5;i++){
                document.write(arr[i]+"<br/>");
            }
        </script>
    </head>
    <body>
    </body>
</html>


The length of the array

The length of the array: it is the total number of elements in the array number.

Continuing Learning
||
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> var arr = ["涛哥" , "男" , 24, "大专" , "北京科技大学" ]; for(var i=0;i<5;i++){ document.write(arr[i]+"<br/>"); } </script> </head> <body> </body> </html>
submitReset Code