Skip to content

Instantly share code, notes, and snippets.

@tararoutray
Last active August 31, 2021 14:25
Show Gist options
  • Save tararoutray/6489adc2d1bb5b81ef6b65b78cd01dc6 to your computer and use it in GitHub Desktop.
Save tararoutray/6489adc2d1bb5b81ef6b65b78cd01dc6 to your computer and use it in GitHub Desktop.
// Let's create a new array of Programming Languages
const programmingLanguages = [
'JS', 'Phython', 'PHP', 'JAVA', 'Kotlin'
];
// Declare an empty variable that will store the programmming languages
// in comma separated format
let languages = '';
// Use the for/of loop to iterate through the array of values
for (let singleLanguage of programmingLanguages) {
languages += singleLanguage + ', ';
}
// Upon printing to console, we will get the following output:
// JS, Phython, PHP, JAVA, Kotlin
console.log(languages);
// It is also possible to print out the index associated with the index elements using the entries() method.
// Loop through both index and element
for (let [index, singleLanguage] of programmingLanguages.entries()) {
console.log(index, singleLanguage);
}
// Above will output the following:
// 0 "JS"
// 1 "Phython"
// 2 "PHP"
// 3 "JAVA"
// 4 "Kotlin"
// A string can also be iterated in the same way as an array.
// Let's assign a string to a variable
let hero = 'Super';
// Iterate through each index in the string
for (let heroCharacter of hero) {
console.log(heroCharacter);
}
// Above will output the following:
// S
// u
// p
// e
// r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment