Loops

Javascript provides loops to handle repeated tasks. Loops are one of the main control structures of JavaScript. Loops can be run repeatedly as per user's discretion. There are many ways to loop through the piece of code.  

WHILE LOOP:

while loop executes a statement or code block repeatedly as long as an expression is true. Once the expression becomes false, the loop terminates.


FOR LOOP: 

Create a for loop to display the numbers from one to five.

<p id="p1"></p>

<script>

var displayNumber = ["One", "Two", "Three", "Four", "Five"];

var len = displayNumber.length;

var text = "";

for (i = 0; i < len; i++) {

  text += displayNumber[i] + "<br>";

}

document.getElementById("p1").innerHTML = text;

No comments:

Post a Comment

Date Object

Purple: Style Blue: HTML Green: JavaScript  <!DOCTYPE html> <html> <style> #dates {   font-family: "Trebuchet MS...