zoukankan      html  css  js  c++  java
  • JavaScript中数组的特点

    1. JavaScript数组中的默认存储值是undefined,其它编程语言数组的默认存储值是0或者是垃圾数据

    2. 与其它的编程语言不同,JavaScript可以访问数组中不存在的索引,会返回undefined,而其它的编程语言会报错或返回垃圾数据

    3. JavaScript可以存储不同类型的数据,而其它的编程语言只能存储一种数据类型的数据

    4. 当JavaScript中数组的存储空间不够用时,它会自动扩容,而其它的语言数组的大小是固定的,一旦定义了,就无法改变

    5. JavaScript中分配给数组的存储空间是不连续的,而其他编程语言中分配给数组的存储空间是连续的


    示例代码

    <script>
            //数组中存储的默认值为undefined
            let arr = new Array(3);
            console.log(arr[0]);
            console.log(arr[1]);
            console.log(arr[2]);
    
            //访问数组中不存在的索引的值会返回undefined
            console.log("arr[7]: " + arr[7]);
    
            //数组中可以存储不同类型的数据
            let arr1 = [1, "hello", true, null, undefined];
            console.log(arr1);
    
            //当数组的存储空间不够时,数组会自动扩容
            let arr2 = new Array(3);
            arr2[0] = 1;
            arr2[1] = 2;
            arr2[2] = 3;
            arr2[3] = 4;
            console.log(arr2);
        </script>
    

    运行结果
    在这里插入图片描述

  • 相关阅读:
    AcWing 204. 表达整数的奇怪方式 / Strange Way To Express Integers
    Codeforces Edu Round 67 A-C + E
    Codeforces Edu Round 66 A-E
    Codeforces Edu Round 65 A-E
    Codeforces Edu Round 64 A-D
    Codeforces Edu Round 63 A-E
    Codeforces Edu Round 62 A-E
    Codeforces Edu Round 61 A-C + F
    python 线程池和锁
    python 线程
  • 原文地址:https://www.cnblogs.com/TomHe789/p/12603068.html
Copyright © 2011-2022 走看看