JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 3-Loops

For Loops

For Statement Loops

The For Statement Loop repeats until a specified condition is FALSE. This is what happens when a for statement loop is running:

  1. The variable (or index) section is executed (i.e. var i = 1)

    This expression usually sets a counter for the loop to run or declare a specific variable.

  2. The conditional section is evaluated (i.e. i <= 10)

    If the value is TRUE, the loop continues to run; when it is FALSE (i.e. > 10), the for statement loop terminates. If the conditional section is omitted, the default is TRUE. Note: omitting the conditional section may lead to an infinite loop. An infinite loop will do just what you think, it will continue on indefinitely. Infinite loops will cause your application to crash, and may even cause your browser to freeze, sometimes forcing you to kill your browser process. It is best to avoid them.

  3. The statement executes (i.e. console.log('Number:', i);).

  4. The increment section is executed (i.e. i++).

    With this, you can designate if the value is added, subtracted, incremented in 2x, etc.

//for statement loops

for ( var i = 1/*variable/index section*/; i <= 10/*conditional section*/;i++/*increment section*/){
    console.log("Number:", i);
}

//Practice
for(var i = 0; i <= 50; i+=5){
    console.log(i);
}

for (var i = 20; i > 1; i--){
    console.log(i);
}

//write a for loop that starts at 3, and increments by 5, but doesnt go over 30
for(var i=3;i<30;i+=5){
    console.log(i)
}
Previous3-LoopsNextFor In Loops

Last updated 7 years ago