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
  • for Loop
  • while Loop
  • forEach method
  • File Location
  • Practice
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 9 Arrays

Iterating

Iteration is the act of repeating an action. With arrays, you'll usually be iterating to print some or all elements, or searching for a particular value or values. There are several different loops that can be used to accomplish this task. For the purposes of these examples, we will use the following array:

let newArray = ['a', 'b', 'c', 'd', 'e'];

for Loop

for (let i = 0; i < newArray.length; i++) //i increases after each iteration; forces the loop to break once i reaches the value of newArray.length
{
    console.log(newArray[i]); //prints the value of the element at index i each iteration
}

while Loop

while (let i < newArray.length)
{
    console.log(newArray[i]);
    i++; //i increases after printing each time
}

forEach method

//standard syntax
newArray.forEach(function(index) {
    console.log(index);
});

//arrow function
newArray.forEach(index => console.log(index));

File Location

We will be working in the following file:

    javascript-library
        └── 0-PreWork
        └── 1-Fundamentals
            └── 1-Grammar-and-Types
            └── 2-ControlFlow-and-ErrorHandling
            └── 3-Loops
            └── 4-Functions
            └── 5-Expressions-and-Operators
            └── 6-Numbers-and-Dates
            └── 7-StringMethods
            └── 9-Arrays
                01-array-review.js
                02-populating-referring.js
                03-length.js 
                04-iterating.js <----You will be working in this file.

Practice

  1. Create a new array with 5 elements. Use each of the above to print each element to the console.

  2. Create a new blank array. Use one of the types of loops to iterate over the first array and push each value into the second array.

  3. Try to iterate over the first array with a do/while loop. Print each element to the console.

PreviousLengthNextMethods

Last updated 7 years ago