- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
For Loops in Javascript
Let’s start with the for loop. There are 2 variations of the for loop in js. The first form is the init, condition, expr loop. This initializes the first statement, then on each iteration executes the expr and checks the condition.
Example
For example,
var step; for (step = 0; step < 5; step++) { console.log('Taking step ' + step); }
Output
This will give the output −
Taking step 0 Taking step 1 Taking step 2 Taking step 3 Taking step 4
There is another form of the for loop, the for in loop. The for...in statement iterates a specified variable over all the enumerable properties of an object. For each distinct property, JavaScript executes the specified statements. For example,
Example
let person = { name: "John", age: 35 }; for (let prop in person) { console.log(prop, a[prop]); }
Output
This will give the output −
name John age 35
Advertisements