JavaScript Loops
Last Updated Jul 29, 2015, 07:00:14 PM
JavaScript Loops
Loops are used to perform repeatable tasks in programming without writing the same logic for each iteration. JavaScript has different types of loops which are used to perform various tasks. Let us look at each javascript loop with examples
The while
loop is used to exectue a specific statement as long as the test condition evolutes to true . The condition is evaluated before executing the statement.
Values
conditionThe test condition to evaluate the execution of the statement.
statementThe key statement to execute until the condition becomes true
ExampleEach iteration, the loop increments n and adds it to m. Therefore, m and n take on the following values:
After the first pass: n = 1 and m = 1
After the second pass: n = 2 and m = 3
After the third pass: n = 3 and m = 6
After the third pass: n = 4 and m = 12
After completing the third pass, the condition n < 10 is no longer true, so the loop terminates.
Another JavaSript While loop Example
Try It Now
JavaScript do while loop
The do...while
statement executes the loop statement until the condition becomes false. The do while loop must execute each statement at least once.
statement
A statement that is executed at least once
conditionThe test condition to evaluate the execution of the statement.
Example
In the above example, the do...while loop iterates at least once and reiterates until x is no longer less than 10.
Try It Now
JavaSript For Loop
The for statement
allow the code to be executed repeatedly . For loops are often used to iterate over a list of itemes
Syntax
initialization
Initial expression to start the loop execution, typically a counter value or variable.
conditionThe test condition to specify when to terminate the loop or how long does the loop should run.
final-expressionFinal expression typically, ends the iteration each time by incrementing or decrementing the initial value
statementThe key statement to execute the loop until a specific condition becomes true.
Example
In the above example, the variable n initialized with 0, it checks if the variable n is less than 9 then it will print that variable while the value of n will keep increased until the condition becomes false
Try It Now
JavaScript Break Statement
The break
statement terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.
The break statement includes an optional label that allows the program to break out of a labeled statement.
SyntaxExample
Try It Now
JavaScript Continue Statement
The continue
statement terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration
Syntax
The continue
statement can include an optional label that allows the program to jump to the next iteration of a labeled loop statement instead of the current loop. In this case, the continue
statement needs to be nested within this labeled statement.
Example
Try It Now
Practice with our Interactive Editor and take your JavaScript Skills to the next level
Exercise 1 Exercise 2 Exercise 3 Exercise 4 Exercise 5 Exercise 6
Browser compatibility
Feature | |||||||
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
Sources and Credits
The source of the content has been referred and updated with Mozilla Developer Network and W3C Organization
Last Updated Jul 29, 2015, 07:00:14 PM
Complete Beginner to JavaScript Developer - Udemy
Advanced JavaScript Development - Udemy
Learn javascript in one month from experts -OneMonth
Become a Certified Web Developer - Udemy