JS-152-Objects-Arrays
  • Introduction
  • 04-Objects
    • OOP-Basics
      • Overview
      • Coding the Example
    • Prototypes
      • Object Prototypes
      • Prototypes Continued
    • Inheritance
      • Inheritance
    • Synopsis
      • Synopsis
    • Challenges
    • Solutions
      • Constructor Function
        • Person Constructor
        • Explanation
      • Inheritance Challenge
        • Code
        • Explanation
      • Family Challenge
        • Child
        • Pet
  • 05-Arrays
    • Array Basics
      • Array Overview
      • Creating an Array
      • Individual Elements and Length
      • Iterating Over Arrays
    • Array Methods
      • Methods Overview
      • .join() Method
      • .reverse() Method
      • .split() Method
      • .replace() Method
      • .splice() Method
      • .map() Method
      • .indexOf() Method
      • .filter() Method
      • .every() Method
    • Palindrome Algorithm
    • Array Challenges
    • Solutions
      • First and Last
        • First Element
        • Last Element
      • Most Frequent
      • Largest and Smallest
        • Single Array
        • Multiple Arrays
      • Missing Number
      • "Arrays" in the DOM
      • Sorting
        • Bubble Sort
        • Selection Sort
    • Resources
Powered by GitBook
On this page
  1. 05-Arrays
  2. Array Basics

Iterating Over Arrays

A common thing to do with arrays is to iterate over each of the values of an array individually. There are two ways to do so: 1. A for() loop.

var emp = ['Joe King', 'Amanda Huggenkiss', 'Justin Thyme'];
for (var i = 0; i < emp.length; i++) {
  console.log(emp[i]);
}

Output:

Joe King
Amanda Huggenkiss
Justin Thyme
  1. A forEach() loop.

    var emps = ['Joe King', 'Amanda Huggenkiss', 'Justin Thyme'];
    emps.forEach(function(emp) {
     console.log(emp);
    });

    We can simplify the code above using an arrow function!

    var emps = ['Joe King', 'Amanda Huggenkiss', 'Justin Thyme'];
    emps.forEach(emp => console.log(emp));

    Our output is the same for all three actually.

Now that you're comfortable with the basics of arrays, we'll move on to array methods.

PreviousIndividual Elements and LengthNextArray Methods

Last updated 7 years ago