Search within TutorialsPark

Javascript Loops

Javascript Looping Statements are used to repeat a portion of code over and over.

Javascript Loops

Javascript Looping Statments are used to repeat a block of code when test condition is found to be true

In Javascript Loops are created using two statements, the while statement and for statement. The following are the type of looping constructs:





Javascript Looping Statement: while

The Javascript while loop executes the code block within its braces until its test condition evaluates to true (i.e non-zero)

The interpreter evaluates the condition every time the before the while loop is executed, if false then the control moves to the statement right after the closing curly braces .

Syntax: while loop

Example: Javascript Loop Statement- while

Give it a TRY! » Note: If the condition remains true forever then it creates an infinite loop.

Javascript Loop Statements: do while

The Javascript condition do/while do while loop continues to execute a block of code repeatedly until the test condition is evaluated to false.

The structure of do/while loop is fundamentally different than while loop.

In do/while loop the condition is evaluated after executing the code, while its opposite with while loop.

Syntax: Javascript - do/while loop

Example: Javascript Loop Statement - do while

Give it a TRY! » Note: The else statement must always be used after an if expression

Javascript Loop Statements: for

The Javascript for loop is a simplified form of looping statement in which the conditions are initialized, evaluated and changed in a compact form.

A standard for statement consists of three clauses in parentheses:

The first clause initializes the expression.

The second clause sets the test condition for the loop to be executed.

The third clause is used to increment the counter of the loop

Syntax: Javascript for loop

Example: Javascript Looping Statement for

Give it a TRY! » Note: The for loop is simple to use compared to while loop.

Javascript Loop Statement: for/in

Javascript for/in loop is used to work with arrays and objects.It executes the code block for each element in the array.

It enables the user to loop through each element in the array with having to know the number of element the array actually contains

Rather than working with index number of each element, it automatically moves to the next index with each iteration.

Syntax: Javascript for/in loop

Example: Javascript for/in looping statement

Give it a TRY! »

Javascript Statements: Break

Javascript Statements break and continue is used to achieve greater control over the execution code within a loop.

The break statement is used to exit the loop immediately, and thus forcing execution to continue with the statements after the loop.

Example: Javascript Statement - Break

Give it a TRY! »

Javascript Loop Statements: Continue

The continue statement is used to exit the loop immediately, but unlike the break statement, the execution continues from the top the loop.

In the below demo, the loop executes for a total of 8 times, it exits once at i = 5, and then begins executing from the top of the loop and executes a total of 8 times and not 9 as one increment does not happen after i = 5.

Example: Javascript Statement - Continue

Give it a TRY! » Note: The final value is 8 instead of 9 as one increment does not happen because of continue statement.

Javascript Labels

Javascript labels are used to name control statements(while, do while, for, for/in ) so that they can be refered by that name elsewhere in the code later on.

Labels are optional, and are used to control the flow of loops.The label name should not be a reserved word

Example: Javascript Labels

Give it a TRY! » Note: Labeling a loop means giving the loop its own name, hence having a greater control.

Javascript : Nested Loops

Nested Loops : Nested loops are created by having one loop within the another,it is most commonly used to display data in a matrix(rows and columns).

The outer loop is first initialized and tested , the inside loop then iterates through all its cycles, the process is repeated till the outer loop condition is false.

Example: Javascript Nested Loops

Give it a TRY! » Note: The inner loop runs more no of times than the outer loop.