top
April flash sale

Search

JavaScript Tutorial

The Javascript for loop is the most used loop in any programming language. It is because it includes three things which other loops like while have to do in a separate statement.Any loop like the while loop below have three parts –var counter = 10; //loop initialization while(counter > 0) { //test statement  console.log("Counter is ", counter);  counter--; //iteration statement }Loop initializationHere we initialize our variable to have an initial value.Test statementThe statement tests whether our condition is true. We generally check the condition which has some test on the variable declared in above loop initialization.Iteration statementA very important part of a loop. It either increment or decrement or loop initialization variable. Generally done from inside the loop and because of it, the loop runs for many times.Now, in a for loop, we can have all these three steps inside it. We will refactor the while loop to a for a loop.for(var counter=10; counter > 0 ; counter--) {  console.log("Counter is ", counter); }//Output -//Counter is 10  //Counter is 9  //Counter is 8  //Counter is 7  //Counter is 6  //Counter is 5  //Counter is 4  //Counter is 3  //Counter is 2  //Counter is 1Just one thing to keep in mind is that the initialization(var counter=10) is done only once. Then the counter is decremented by 1 and then the test condition(counter > 0) is checked.This is repeated till counter becomes 0 and the test condition becomes false and we get excited from the loop.
logo

JavaScript Tutorial

for loop

The Javascript for loop is the most used loop in any programming language. It is because it includes three things which other loops like while have to do in a separate statement.

Any loop like the while loop below have three parts –

var counter = 10; //loop initialization
while(counter > 0) { //test statement
 console.log("Counter is ", counter);
 counter--; //iteration statement
}

Loop initialization

Here we initialize our variable to have an initial value.

Test statement

The statement tests whether our condition is true. We generally check the condition which has some test on the variable declared in above loop initialization.

Iteration statement

A very important part of a loop. It either increment or decrement or loop initialization variable. Generally done from inside the loop and because of it, the loop runs for many times.

Now, in a for loop, we can have all these three steps inside it. We will refactor the while loop to a for a loop.

for(var counter=10; counter > 0 ; counter--) {
 console.log("Counter is ", counter);
}

//Output -

//Counter is 10 
//Counter is 9 
//Counter is 8 
//Counter is 7 
//Counter is 6 
//Counter is 5 
//Counter is 4 
//Counter is 3 
//Counter is 2 
//Counter is 1

Just one thing to keep in mind is that the initialization(var counter=10) is done only once. Then the counter is decremented by 1 and then the test condition(counter > 0) is checked.

This is repeated till counter becomes 0 and the test condition becomes false and we get excited from the loop.

Leave a Reply

Your email address will not be published. Required fields are marked *

Comments

Janani

I have learned many things from this article. It is beneficial for me. Thank you!

Nilesh Chakrabarty

Nice example for beginners.. I m a beginner so this is very helpful for me ... so plz give this type of beginners example..

michael

This is a great introduction to variables in JavaScript! As a beginner to JavaScript, I found this guide very helpful in understanding the basics of variables and how they are used in JavaScript.

qtsinfo it

Thanks for sharing the information, it is very helpful, I hope to get more such beautiful blogs from you.

knowledge-wisdom

You have shared great information with me i appreciate your work!

Suggested Tutorials

Node JS Tutorial

Build various types of web applications,command-line application,etc.
Node JS Tutorial

Build various types of web applications,command-line application,etc....

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More