Skip to content

Instantly share code, notes, and snippets.

@1kohei1
Created January 12, 2019 17:16
Show Gist options
  • Save 1kohei1/9f68300b86367bdcbe14e8e5e1e58874 to your computer and use it in GitHub Desktop.
Save 1kohei1/9f68300b86367bdcbe14e8e5e1e58874 to your computer and use it in GitHub Desktop.
var a = [];
a[2] = 1;
a.length; // 3
a; // [undefined, undefined, 1]
// You can set the key value pair to the array, but it doesn't count in length
a.a = 12;
a; // [undefined, undefined, a, a: 12];
a.length; // 3
a.a; // 12
// However, if string key can be converted to the base-10 number, it will be conveted and used as a index
a['4'] = 4;
a.length; // 5
a; // [undefined, undefined, 1, undefined, 4, a: 12]
// However, coercion doesn't apply to hexadecimal
a['0x00'] = 1;
a.length; // 5
a; // [undefined, undefined, 1, undefined, 4, a: 12, 0x00: 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment