ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
JavaScript Tutorial - Index
JavaScript Tutorial - Introduction to JavaScript
JavaScript Tutorial - Basic JavaScript Concepts
JavaScript Tutorial - Working with Operators and Variables
JavaScript Tutorial - How to work with Loops and Functions?
JavaScript Tutorial - How to work with Forms and Events?
JavaScript Tutorial - How to work with Objects?
JavaScript Tutorial - How to work with Browsers and Cookies?
JavaScript Tutorial - More about Objects
JavaScript Tutorial - How to validate Forms with JavaScript?
JavaScript Tutorial - Advanced Techniques in JavaScript
     

 
Online Personal Trainer Certification
Personal Trainer Certification (Spanish)
Six Sigma Certification
Find a Personal Trainer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Master Black Belt Certification
Six Sigma Green Belt Certification
Life Coach Certification
Lean Management Certification
Lean Six Sigma Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
Business Analyst Certification
SEO Certification
Online Business Writing Course & Certification
Makeup Artist Certification
Baby Sitting Course & Certification
Time Management Certification
Health Club Management Certification
Medical Coding Certification
Medical Billing Certification
Business Analysis Course
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
Event Management Certification
Test Search by Job Title
Administrative Assistant Certification
Virtual Assistant Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > JavaScript Tutorial > Working with Loops and Functions
 

JavaScript Tutorial - Working with Loops and Functions

 
Page 1 of 4
 
Overview
 
In this chapter you will learn :
  • Importance of looping in programming.
  • Different loops available for different situations.
  • Break and continue statements.
  • Functions used in JavaScript.
  • Creating User-defined functions.
  • Passing parameters in a function.
  • Calling a function.

 

 

Working with Loops and Functions

Looping refers to the ability of a block of code to repeat itself. JavaScript performs several types of repetitive operations. Using these operations you can iterate through the list of array elements, repeat certain operations until some condition is met, or just keep repeating a certain operation. Loops are also known as control structures. JavaScript offers three types of loop structures:

For Loop

The For loop iterates through a block of statements for a specified number of times until a condition is met. For loop is used when it is known in advance that how many times a particular JavaScript code should run.

Syntax
for ( initialize ; condition ; update )
                                                  {
                                                                JavaScript Statements;
                                                   }

                        
The initialize expression, sets up a counter variable and assigns the initial value. The initialize expression is executed exactly once when the for statement is first encountered. It is the starting point of the loop. The declaration of the counter variable can also be done here. The 'condition' tests the variable for a certain condition. The loop terminates when this condition evaluates as false, i.e., the loop fires till condition evaluates to true. The 'update expression' determines how the variable is incremented. It specifies how the initial value in initialize expression is incremented. This increment factor can be negative also.

Example
/* The following code prints the digits from 1 to 10 */
for ( var num = 1; num<=10 ; num++ )
{
     document.write (num);
}

 

While Loop

The while loop is used when you want the loop to execute infinitely and continue executing  until  the condition fails.

Syntax
while ( condition )
                                                          {
                                                                     JavaScript Statements;
                                                           }

                     
The While loop test the condition for its validity and continues to execute JavaScript statements as long as the condition is true.

Example
/* The following code prints the digits from 1 to 10, using while loop*/
var num = 1;
while ( num <= 10 )
{
          document.write( num );
          num ++;
}

Do - While Loop

The do-while loop is quite similar to the while loop. The only difference between these two loops is that the do-while  loop will execute a block of code only once, whether the condition is true or not whereas the while loop will only execute if the condition is true.

Syntax
do
                                                  {
                                                              JavaScript Statements;
                                                  }
                                                 while ( condition )

                       
In the following example, you can see that even though the condition is false, but the statements inside the 'do' block will execute once.

Example
/* The following code prints number 12*/
num = 12;
do
{
           document.write ( num );
           num++;
}
while ( num <= 10 )

Other conditional constructs in JavaScript which offers easy decision making in JavaScript programs are:

 

     

 

 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2015. All Rights Reserved.