Skip to content

Instantly share code, notes, and snippets.

@digitalconceptvisuals
Created July 31, 2020 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalconceptvisuals/43f28fefe753d8b31c3e7a0209880d6f to your computer and use it in GitHub Desktop.
Save digitalconceptvisuals/43f28fefe753d8b31c3e7a0209880d6f to your computer and use it in GitHub Desktop.
// 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