-
-
Save digitalconceptvisuals/43f28fefe753d8b31c3e7a0209880d6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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