for...of
const arr = [1, 2, 3, 4, 5]
for (const num of arr) {
console.log(num);
}
The for...of loop is ideal for iterating over iterable objects like arrays, strings, and Maps.
Console Output:
1
2
3
4
5
const greetings = "Hello world!"
for (const greet of greetings) {
console.log(`Each char is ${greet}`)
}
Console Output:
Each char is H
Each char is e
Each char is l
Each char is l
Each char is o
Each char is
Each char is w
Each char is o
Each char is r
Each char is l
Each char is d
Each char is !
Map
A Map is a collection of key-value pairs where both keys and values can be any data type.
const map = new Map()
map.set('IN', "India")
map.set('USA', "United States of America")
map.set('Fr', "France")
map.set('IN', "India")
console.log(map);
//Iterating KEY and VALUES at once
for (const [key, value] of map) {
console.log(key, ':-', value);
}
map.set()
method adds new key-value pairs to the map.Console Output:
Map(3) { 'IN' => 'India', 'USA' => 'United States of America', 'Fr' => 'France' } IN :- India USA :- United States of America Fr :- France