Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Normal usage
console.log("const normalArray = [11, 23, 35];");
const normalArray = [11, 23, 35];
console.log(normalArray);
/* [ 11, 23, 35 ] */
// Adding by index
console.log("normalArray[3] = 48;");
normalArray[3] = 48;
console.log(normalArray);
/* [ 11, 23, 35, 48 ] */
// Non positive index get added as keys
console.log("normalArray[-1]=5");
normalArray[-1] = 5
console.log(normalArray);
/* [ 11, 23, 35, 48, '-1': 5 ] */
// Even string
normalArray['foo'] = 'bar';
console.log("normalArray['foo'] = 'bar';")
console.log(normalArray);
/* [ 11, 23, 35, 48, '-1': 5, foo: 'bar' ] */
// for of gives only positive index
console.log("for (let index of normalArray)");
for (let element of normalArray)
console.log(element);
/*
11
23
35
48
*/
// for in gives ALL key/value pairs
console.log("for (let index in normalArray)");
for (let index in normalArray)
console.log(`${index}: ${normalArray[index]}`);
/*
0: 11
1: 23
2: 35
3: 48
-1: 5
foo: bar
*/
// It counts only positive index
console.log("normalArray.length");
console.log(normalArray.length);
/* 4 */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment