Home  >  Article  >  Web Front-end  >  Does javascript array automatically expand?

Does javascript array automatically expand?

WBOY
WBOYOriginal
2022-06-29 17:19:282121browse

The array in JavaScript is automatically expanded; the array is specially used to store a set of data. When the storage space of the array in JavaScript is not enough, the array will automatically expand, while the size of the array in other languages ​​is Fixed, once defined it cannot be changed.

Does javascript array automatically expand?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

Does JavaScript arrays automatically expand?

1. The default storage value in JavaScript arrays is undefined, and the default storage value in arrays in other programming languages ​​is 0 or garbage data

2. Unlike other programming languages, JavaScript can access non-existent indexes in the array and will return undefined, while other programming languages ​​will report errors or return garbage data

3. JavaScript can store different types of data. Other programming languages ​​can only store one data type of data

4. When the storage space of the array in JavaScript is not enough, it will automatically expand, while the size of the array in other languages ​​is fixed. Once defined , it cannot be changed

5. The storage space allocated to arrays in JavaScript is discontinuous, while the storage space allocated to arrays in other programming languages ​​is continuous

Examples are as follows:

<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>

Output results:

Does javascript array automatically expand?

Extended knowledge:

1. What is an array?

Array is specially used to store a set of data

Note: It is different from the Number/String/Boolean/Null/undefined we learned earlier (basic data type), and The array (Array) we are learning today is not a basic data type, but a reference data type (object type)

2. How to create an array?

let 变量名称= new Array(size)

3. How to operate arrays?

3.1 How to store data in an array

 变量名称[索引号] = 需要存储的数据;

3.2 How to get stored data from an array

变量名称[索引号];

[Related recommendations: javascript video tutorialweb front end

The above is the detailed content of Does javascript array automatically expand?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn