Counter using setInterval function in JavaScript

Of course, it's not very interesting that our timer outputs the same thing every time. Let's complicate our task and make it so that every second the numbers are displayed in the console in ascending order: first 1, then 2, then 3 and so on.

To do this, we need a counter variable that will store its values between function runs. It's easy to see that you can just make a global variable:

let i = 0; // the global variable setInterval(function() { i++; console.log(i); }, 1000);

Let's rewrite it more compactly:

let i = 0; setInterval(function() { console.log(++i); }, 1000);

Or even more compactly using an arrow function:

let i = 0; setInterval(() => console.log(++i), 1000);

Let a variable be given that initially stores the number 100. Start a timer that every second will decrease the value of this variable by 1 and print this value to the console.

enru