欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Array对象的多样性。面试题

程序员文章站 2024-03-03 16:10:34
...
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
</body>
<script>
    var arr=new Array(1)
    var arr1=new Array(2)
    var arr2=new Array(1,2)
    var arr5=new Array('abc')
    var arr6=new Array([0])  //单个非数值。比如字符串,布尔值,对象
    var arr7=new Array(0)  //单个非数值。比如字符串,布尔值,对象
    var arr8=new Array(true)  //单个非数值。比如字符串,布尔值,对象
    var arr3=new Array(3.2)
    var arr4=new Array(-3)

    console.log(arr3.length);  //invalid array length
    console.log(arr4.length); //invalid array length

    console.log(arr5); //[abc]
    console.log(arr6); //[Array[1]]
    console.log(arr7); //[]
    console.log(arr8); //[true]

    console.log(arr.length);// 1
    console.log(arr); //[empty]

    console.log(arr1.length);// 2
    console.log(arr1); //[empty*2]

    console.log(arr2.length);// 2
    console.log(arr2); //[1,2]

</script>
</html>
// bad
var arr = new Array(1, 2);

// good
var arr = [1, 2];