Web Programming TutorialsImplementing Loops in JavaScript

Implementing Loops in JavaScript

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 :

  1. 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>
    
  2. After writing the code your Notepad++ window will have the following look :
  3. v2

    v3

  4. So now when you run it on the browser you will have the following output :
  5. v4
    v5

  6. Thus we had a brief look over implementing loops in javascript .

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -