JS For In LoopFor loop example: 1 2 3 4 5 6 var sum = 0; for (var i=1; i<=100; i++) { sum += i; } alert(sum); //5050 You may use break to jump out of the loop:1 2 3 4 5 6 7 var sum = 0; for (var i=1; i<=100; i
Initialization is the first step in theforloop process where you define the loop variable to a starting value. This will be executed at the beginning of the loop. The main purpose of the initialization is to initialize the variable that will be used in theforloop. Example: for (let i = ...
which means that it takes the command right after the loop, without executing the rest of it, but instead doing whatever code is next. Here’s an example of how you can use it:
log(counter + ' - Inside for loop on TechOnTheNet.com'); } console.log(counter + ' - Done for loop on TechOnTheNet.com'); In this for loop example, the loop will continue as long as counter is less than 5 as specified by: counter < 5; The for loop will continue while ...
JavaScript for...in With Strings You can also use thefor...inloop to iterate overstringvalues. For example, conststring ='code';// using for...in loopfor(letiinstring) {console.log(string[i]); }; Run Code Output c o d e
Interested to learn more about for loop in JS? Then check out our detailed Example where we explain every type of this loop in JavaScript!
Here is an example:const user = { name: 'John Doe', email: 'john.doe@example.com', age: 25, admin: false } for (const key in user) { console.log(key) // property name console.log(user[key]) // property value } for...in loop examples...
You can omit expression 1 when your values are set before the loop starts: Example leti =2; letlen = cars.length; lettext =""; for(; i < len; i++) { text += cars[i] +""; } Try it Yourself » You can intiate many values in expression 1 (separated by comma): Example...
The JavaScript for/in statement loops through the properties of an object: Example varperson = {fname:"John", lname:"Doe", age:25}; vartext =""; varx; for(xinperson) { text += person[x]; } Try it yourself » The While Loop ...
"array value: 1" "array value: 2" "array value: 3" "array value: a" "a is string at index: 3,loop stoped using return" We’ve achieved the same result as the first example by using the return keyword. We have placed that for loop code in the function’s body and used the ...