In this session we will study the LOOPING statements of java script which includes for , while , do while , for in loops which are illustrated in the given tutorial.
- So to implement the loops in javascript follow the steps given below :
- So to implement the loops write the following code in Notepad++ .
<!Doctype html> <html> <head> <title>Javascript Loops</title> </head> <body> <h1>Thi is the dowhile loop</h1> <script> var people = ['Sally','Bob','Steve','Jen']; i = 0; while(i < people.length) { document.write(people[i]+'<br />'); i++; } </script> <h2>This is the for loop</h2> <script> var i; for(i=0;i<10;i++) { document.write("Number is" +i+" <br />"); } </script> <h1>This is the for in loop </h1> <script> var people = ['Sally','Bob','Steve','Jen']; for(i in people) { document.write(people[i]+'<br />'); } </script> <h1>This is the while loop</h1> <script> var i = 0; while(i <= 10) { document.write('number '+i+'<br />'); i++; } </script> </body> </html>
- After writing the code your Notepad++ window will have the following look :
- So now when you run it on the browser you will have the following output :
- Thus we had a brief look over implementing loops in javascript .