Array con For Loop. JS
Hola!
Voy a ir mezclando palabras en inglés con la explicación porque es bueno saber los conceptos principales en inglés ya que es el idioma universal para la programación.
Iterate Through an Array with a For Loop
Una tarea bastante común en JavaScript es to iterate a través de los contenidos de un array. Una de las tantas maneras de hacerlo es con un for loop. Este code saca cada elemento del array a la console:
const arr = [10, 9, 8, 7, 6];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
Los arrays son ZERO-BASED indexing, que significa que empieza contando por 0, 1, 2, ... por lo tanto el último index de la array es length -1.
const arr = [10, 9, 8, 7, 6]
en este caso 10 es index [0], 9 - [1], 8 - [2], 7 - [3], 6 - [4]
o lo que es lo mismo 6 es length - 1 (5 - 1 = 4)
Nuestra condición para este loop es entonces que i < arr.length, por lo que el loop para cuando i es igual a la length. En este caso la última iteration es i === 4, es decir, cuando i es igual a arr.length - 1 y sacará en la console el número 6.
Entonces i aumenta a 5, y el loop acaba aquí porque i es < arr.length es falso.
Si tenéis cualquier duda me lo decís. Sé que he saltado todo lo básico pero esta clase y un poco más avanzado de la siguiente la ví hace poco y al tenerla fresca quería publicarla. Iré colgando los básicos también.
Leave Array con For Loop. JS to:
Read more #coding posts
Best Posts From Ramón
We have not curated any of ramon2024's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From Ramón
- Enchanted place - A MEDIEVAL WALK through albarracin continues
- A MEDIEVAL WALK THROUGH ALBARRACÍN
- I built a tool LIMITLESSHUB for Limitless players, and here's why
- Seneca's 5 Lessons on Time That Will Change How You Live
- Carl Jung's 6 Shadows You Need to Face to Find Peace
- Secret to Overcoming Anxiety (Used by Marcus Aurelius)
- Marcus Aurelius' 7 Habits That Will Rewire Your Brain
- The philosophy that changed how I think about stress
- State of Art #1 — Seeing Differently EN-ES
- Textures of Valencia (EN-ES)
- Closing 2025 Between Valencia and Jàvea (EN-ES)
- HEAVY SKY, QUIET MIND (EN-ES)
- THE SHAPES THAT STOPPED ME (EN-ES)
- Lanzarote or Mars? A Solo Adventure (EN-ES)
- 👁️ Beyond the Mirror: Expanding the Experiment
- A Cemetery of Stories: Fragments in Monochrome (EN-ES)
- Fractured symmetry experiments
- BARCELONA IN FULL COLOR: LIGHTS, MARKETS, ROOFTOPS & MAGIC (EN-ES)
- MY NOVEMBER RECAP: CREATIVITY, PROGRESS & NEW BEGINNINGS (EN-ES)
- Mountains, Trains, Jungles and One Last Night in Sri Lanka (EN-ES)