The javascript Array features you don't know

Source: Internet
Author: User
Tags javascript array

At work, we often use js arrays. But have you seen the following? 1. Text Subscript: 1 var a = []; 2 a [-1] = 1; have you ever wondered if the subscript of an array is negative? The subscript of the array starts from 0. However, the above statements can still be written. However, please refer to: 1 console. log (. length); // 02 console. log (a [-1]); // 13 console. log (a ["-1"]); // 14. hasOwnProperty (-1); // true indicates that the object property is stored here. 2. Two-dimensional array: copy the Code 1 var a = []; 2 a [] = 1; 3 a [] = 2; 4 a [] = 3; 5 a [1, 1] = 4; 6 7 console. log (. length); // 28 console. log (a [0]); // 39 console. log (a [1]); // 4 copying code js itself does not support two-dimensional arrays, so the length above is 2, its internal implementation is to treat the small two-dimensional array as a comma expression, so only one value is valid. If you want to implement a two-dimensional array, you can also: 1 var a = [1]; 2 var B = []; 3 B [0] = a; 4 console. log (B [0] [0]); // 13, object subscript: When the subscript of an array is an object, js will try to convert it to a value. If not, then it is converted into a string. 1 var a = []; 2 var B = {c: 1}; 3 a [B] = 2; 4 console. log (a [B]); // 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.