Creation and ma...LOGIN

Creation and manipulation of arrays in JavaScript

How to create an array


##1. Use the new keyword and Array() to create an array
##var arr = new Array(); //Create an empty array

var arr = new Array("zhou gensheng" , "male" , 30); //Create an array and initialize the elements of the array

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>           
            //创建一个没有任何元素的数组
            var arr = new Array();
            //增加数组元素
            arr[0] = "张三";          
            arr[1] = "男";          
            arr[2] = 25;          
            arr[3] = "安徽";          
            document.write(arr);         
        </script>
    </head>
    <body>
    </body>
</html>



2. Use [] to create

## square brackets [ ], use commas in English to separate multiple values.

The value of the array element can be of any type. Such as: character type, numeric type, Boolean type, array type, object type, function.

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>php.cn</title>
        <script>          
            //使用[]方式创建一个数组
            var arr = ["张三","男",25,"安徽"];         
            document.write(arr);         
        </script>
    </head>
    <body>
    </body>
</html>


Array operations


Read elements: Read elements that already have subscripts. For example: var age = arr[2];
  • Modify element: Modify the value of an element with an existing subscript (reassign a value). For example: arr[2] = 26;
  • Add element: add an element with a non-existent subscript.
  • Delete elements: Using the keyword delete, you can only delete the value of the element, while the subscript is still there.


Next Section
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //使用[]方式创建一个数组 var arr = new Array("张三","男",25,"安徽"); document.write(arr); </script> </head> <body> </body> </html>
submitReset Code
ChapterCourseware