Contenido del Curso
JavaScript Data Structures
JavaScript Data Structures
2. Fundamentals of JavaScript Objects
Understanding Objects in JavaScriptCreating Objects in JavaScriptWorking with Nested Object PropertiesChallenge: Create and Structure an ObjectAccessing Object Properties in JavaScriptChallenge: Retrieve Object Property ValuesManaging Object PropertiesChallenge: Modify and Extend an ObjectDefining Object MethodsUsing Properties within MethodsChallenge: Work with Object MethodsFundamentals of JavaScript Objects Sum-Up
3. Advanced Object Manipulation Techniques
Iterating Over Object Properties with the for...in LoopChallenge: Explore Object Properties with IterationUsing hasOwnProperty() to Check Object PropertiesChallenge: Object Property Iteration with hasOwnProperty()Cloning and Merging Objects with the Spread OperatorChallenge: Combine Objects with the Spread OperatorDestructuring Objects for Cleaner CodeChallenge: Extract Data with Object DestructuringAdvanced Object Manipulation Sum-Up
4. Mastering JavaScript Arrays
Understanding JavaScript ArraysChallenge: Access Array ElementsModifying Arrays and Accessing ElementsChallenge: Modify Array ElementsIterating Over Arrays with the for LoopChallenge: Loop Through Arrays with forUsing the for...of Loop for Array IterationChallenge: Efficient Array Iteration with for...ofMastering JavaScript Arrays Sum-Up
5. Advanced Array Methods and Transformations
Transforming Arrays with the map() MethodChallenge: Modify Array Elements Using map()Filtering Arrays with the filter() MethodChallenge: Select Specific Data Using filter()Finding Elements in an Array with the find() MethodChallenge: Search for Items Using find()Sorting Arrays with the sort() MethodChallenge: Sort and Extract Data with sort()Advanced Array Methods and Transformations Sum-UpCourse Summary
Iterating Over Arrays with the for Loop
Array iteration is a crucial concept when working with arrays. It allows us to process each element in an array, perform operations, and make decisions based on their values. In this chapter, we will explore for
loop for iterating over arrays.
Syntax
The for
loop is a fundamental tool for iterating over an array element by element. It allows us to access each element in the array by element index. The syntax for a for
loop is as follows:
In this syntax:
let i = 0
initializes a loop counter variablei
to zero;i < array.length
defines the condition for the loop to continue. It will run as long asi
is less than the length of the array;i += 1
increments the loop counter after each iteration.
Example
Here's an example of using the for
loop to access and display the elements of the students
array:
const students = ["Brandon", "Mario", "Saul"]; for (let i = 0; i < students.length; i += 1) { console.log(students[i]); }
- Line 1: This line declares an array called students and initializes it with three strings, which represent the students' names. The array contains
"Brandon"
,"Mario"
, and"Saul"
; - Line 3: This line starts a for loop. It has three parts separated by semicolons:
let i = 0;
: This part initializes a variablei
and sets it to0
.i
is used as a loop counter;i < students.length;
: This part is the condition for the loop to continue. The loop will continue as long asi
is less than the length of thestudents
array;i += 1
: This part is the update statement, which increments the value ofi
by1
after each iteration.
- Line 4: Inside the
for
loop, this line uses theconsole.log()
function to log the value at the i-th index of thestudents
array. In the first iteration,i
is0
, so it logs the name at index0
, which is"Brandon"
. In the second iteration, it logs"Mario"
, and in the third iteration, it logs"Saul"
.
1. What is the purpose of a for
loop when working with arrays?
2. What should be the condition for the loop to iterate through the entire computers
array?
3. What should be the condition for the loop to iterate through the computers
array to show only the first two elements?
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 4. Capítulo 5