For Loops in Javascript

JavascriptFront End TechnologyWeb Development

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
raja
Published on 27-Jun-2018 09:35:38
Advertisements