notes-6
notes-6
====
console.log("Print me 5 times");
Types of loops :
--------------
1. for loop :
============
Syntax :
for(initialization; condition; increment/decrement){
// statements
}
Eg.
for (let i = 1; i <= 10; i++) {
console.log("Print me " + i + " times");
}
2. while loop :
==============
Syntax :
while(condition){
// statements
}
Eg.
let i = 1;
while(i <= 5){
console.log(i);
i++;
}
3. do while loop
===============
Syntax :
do{
// statements
} while(condition)
Eg.
let i = 1;
do{
console.log(i);
i++;
} while(i <= 5);
4. for in Loop :
==============
To interate keys of an object or indices of an array
Syntax :
for(let key in obj){
// statements
}
Eg.
const obj = {
name: "Akhil",
age: 28,
city : "Kanpur"
}
for(let key in obj){
console.log(key, obj[key]);
}
5. For of Loop:
==============
To iterate values of an interable(eg. array, string, map, etc)
Syntax :
for(let value of iterable){
// statements
}
Eg.
const numbers = [2,8,3,38,383,476];
for(let index in numbers){
console.log(index);
console.log(numbers[index]);
}