Iterating Over Arrays
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 63 of 77.
Iteration means going through elements one by one in a sequence. With arrays, we can access each element systematically using different methods.
The most common way to iterate through an array is using a for loop together with the .length property:
let fruits = ["apple", "banana", "orange"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}Output:
apple
banana
orangeChallenge
EasyCreate a program that receives an array of strings as input (given), and prints a new array containing only the words longer than 5 characters
Cheat sheet
To iterate through an array, use a for loop with the .length property:
let fruits = ["apple", "banana", "orange"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}Try it yourself
let arr = inp.split(", "); // Don't change this line
// Write your code belowThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False9Functions
Declare a FunctionArgumentsReturnRecap - Sigma FunctionFunction ExpressionDefault ParametersArrow FunctionsRecap - Validation Function12Arrays Part 2
Iterating Over ArraysThe forEach Methodfor...of LoopRecap - P CounterArray SlicingArray Methods Part 3Array Methods Part 4Membership Testing