0% found this document useful (0 votes)
3 views12 pages

JavaScript Loop Types and Examples

The document outlines various types of loops in JavaScript, including while, do...while, for, for/of, and for/in loops. It provides syntax examples for each loop type and includes programming exercises for users to practice, such as generating a multiplication table, calculating factorials, and determining age based on birth year. The document emphasizes the importance of handling user input and conditions within loop structures.

Uploaded by

Tom Tom
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views12 pages

JavaScript Loop Types and Examples

The document outlines various types of loops in JavaScript, including while, do...while, for, for/of, and for/in loops. It provides syntax examples for each loop type and includes programming exercises for users to practice, such as generating a multiplication table, calculating factorials, and determining age based on birth year. The document emphasizes the importance of handling user input and conditions within loop structures.

Uploaded by

Tom Tom
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

LOOPS IN JAVASCRIPT

Loops
JavaScript language supports following loops :

while :loops through a block of code while a specified


condition is true
do…while:also loops through a block of code while a spe-
cified
condition is true
for:loops through a block of code a number of times

for/of:loops through the values of an array

for/in: Newly introduced in ES6 and loops through the


properties of an object
Syntax Of while Loop

while

while(<test
cond>)
{
// loop body
}
PROGRAM

Write a JavaScript code to accept an integer


from the user and print the table of that no up
to 10 terms
Syntax Of do…while
Loop:

do…while

do
{
// loop body
}while(<test
cond>);
PROGRAM

Write a JavaScript code to accept an integer


from the user and calculate it’s factorial
PROGRAM

Write a JavaScript code to accept the birth year


from the user and calculate and print his age.
Also print whether the user can vote or not .
Make sure if the age turns out to be 0 or
negative your code prompts the user to input
his birth year again until the age comes
positive
Syntax Of for Loop

for

for
(stmt1;stmt2;stmt
3)
{
// loop body
}
Various forms of for loop :

Syntax:

for (stmt 1, stm2; stmt 3; stmt


4)
{
//the code block to be
executed
}
Various forms of for loop :

Syntax:

for ( ; stmt 2;stmt 3)


{
//the code block to be
executed
}
PROGRAM

Write a JavaScript code to accept an integer


from the user and calculate and print the sum
of even and sum of odd nos up to the given no
separately

You might also like