-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfour_forIn.js
37 lines (29 loc) · 978 Bytes
/
four_forIn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// For in Loop
// 'For in' on Objects
const myObject = {
js: "javascript",
cpp: "C++",
rb: "ruby",
swift: "swift by apple"
}
for (const key in myObject) {
console.log(`${key} shortcut is ${myObject[key]}`);
} // Output: js shortcut is javascript and so on...
/* Note: For in Loop works on Objects.
----------------------------------------------------------------
'For in' on Array */
const programming = ["js", "cpp", "java", "py"]
for (const key in programming) {
// console.log(key); // Gives array keys from 0 to 3 (index)
console.log(programming[key]);
} /* Ouput: Gives values inside programming array.
----------------------------------------------------------------
'For in' on Map */
const map = new Map()
map.set('IN', "India")
map.set('USA', "America")
map.set('Fr', "France")
for (const [key, value] in map) {
console.log(key, '=', value);
} // Output: Runs but gives not value
// Note: Maps is not iterable in 'for in' loop.