HAKATEMIA
25JavaScript basics

JavaScript Loops

Easy25MIN

Loops are programming structures that repeat a certain part of the code as many times as needed. This allows, for example, the repetition of the same operations multiple times, automation of code execution, and managing many other functions. JavaScript has several different types of loops, but the most common ones are for loops, while loops, and do-while loops.

for loops

For loops are usually used when you know in advance how many times you want to repeat a certain part of the code. The structure of for loops consists of initial conditions, condition statements, and increment statements. For example, you can use for loops to print numbers 1-10:

JAVASCRIPT
1for (var i = 1; i <= 10; i++) {
2  console.log(i);
3}

This defines the initial condition as "i = 1", the conditional statement as "i <= 10", and the increment statement as "i++". The loop repeats the code block 10 times, and with each iteration, the variable "i" increases by one.

TRY ITREPLIT

NOTE

Valitettavasti Replit-palvelu on muuttunut lennosta eikä enää anna suorittaa näitä koodeja suoraan selaimessa. Voit klikata alla olevaa "Open in Replit" linkkiä ja avata koodin Replit-palvelussa. Meillä on työn alla etsiä Replitille korvaaja.

1 / 4
Hakatemia Pro

Learn to hack — start here

Hundreds of interactive courses, virtual labs and CTF challenges in your browser. Start a free trial — no card required.