SoFunction
Updated on 2025-03-08

FAQs on adding and accessing values ​​in arrays in JavaScript

This article introduces some small issues about arrays, which may be helpful to you. The article is not well written, please forgive me.

1.

// var arr = [,,];
// arr["bbb"]="nor ";
// arr[-]="nor ";
// (arr); >> [, , , bbb: "nor ", -: "nor "]
// () >> "nor "

If we want to add a value into the array, add it in the form of []. If we write a negative number or a string, then it is added at the end of the array, and it is added in the form of a key-value pair, so the next time we access this value, you can access it in the form of a point, but if it is a number, you must access it through [].

2.

// var arr = [,,];
// arr["bbb"]="nor ";
// (arr); [, , , bbb: "nor "]
// (arr[]) undefined

If you add a value to the array through a string or a negative number, then the next time you access it, you must also access it through key-value pairs.

3.

// var arr = [,,];
// arr["bbb"]="nor ";
// arr[-]=;
// ();
// (arr); >> [, , , , bbb: "nor "]
// (); >> 

// It is worth noting that the value added by strings or negative numbers will not add its length to the array, and the added method will always be at the end of the array, because when we use the push method to add the number 4, we found that it did not add it to the end. Everyone knows that the push method adds the value to the end of the array. Maybe we can draw a conclusion that is the arrangement of numbers and numbers, key-value pairs and key-value pairs.

This article uses the above three points to briefly analyze common problems of adding values ​​and accessing values ​​in arrays in JavaScript, hoping that they will be helpful to everyone. At the same time, my editor wishes everyone a happy Chinese New Year!