From the course: Computer Science Principles Lab: JavaScript

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use for loops

Use for loops - JavaScript Tutorial

From the course: Computer Science Principles Lab: JavaScript

Start my 1-month free trial

Use for loops

- The for loop starts with the for stapen, and then you need to define the iterator, conditional test and the step increment for the iterator variable. Let's create a for loop that will step through the numbers zero through 24 and find the squares for each number. First we'll start with the for statement then we create a set of parentheses. Inside we first need to create our iterator variable, which is often just called I, and we initially set it to zero. Then we add a semicolon. Next we take the iterator and we create a conditional test. This test must be true before it will attempt to run through the loop of code. We'll test that I is less than 25. Then add another semicolon. Last we need to tell the loop how much we want to change the iterator each time the loop runs. We can use the increment operator to increase it by one each time the loop runs. Then we create the loop body using curly braces. Inside the code block we will create a message for the console log. We'll create a…

Contents