1.5 JavaScript loop statement
The loop statement in JavaScript C++
is similar to that in and also contains for
, while
loop.
for
cycle
for (let i = 0; i < 10; i++) {
console.log(i);
}
When enumerating objects or arrays you can use:
for-in
Loop, you can enumerate the subscripts in the array, and the subscripts in the objectkey
for-of
Loop, you can enumerate the values in the array, and the values in the objectvalue
while
cycle
let i = 0;
while (i < 10) {
console.log(i);
i++;
}
practise
- Find the sum of the cubes of all the numbers from 1 to 100.
- Find the nth term of the Fibonacci sequence. f(1) = 1, f(2) = 1, f(3) = 2, f(n) = f(n-1) + f(n-2).
- Print all prime numbers from 1 to 100.