0% found this document useful (0 votes)
2 views

notes-6

The document provides an overview of different types of loops in JavaScript, including for loops, while loops, do while loops, for in loops, and for of loops. Each loop type is accompanied by its syntax and an example for better understanding. The document serves as a quick reference for using loops in JavaScript programming.

Uploaded by

Kavya Singh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

notes-6

The document provides an overview of different types of loops in JavaScript, including for loops, while loops, do while loops, for in loops, and for of loops. Each loop type is accompanied by its syntax and an example for better understanding. The document serves as a quick reference for using loops in JavaScript programming.

Uploaded by

Kavya Singh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Loop :

====
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]);
}

You might also like