Category: Vanilla Javascript

Added: 11th of October 2022

Viewed: 967 times


Simple for loop in Javascript

What does the script do?
The javascript below outputs the numbers 1 to 10 using a simple for loop.
Set the number of iterations by changing the variable let n={number}

Create a new file on your Desktop named forloop.html, copy and paste the code below then save the file. Launch the file in your browser.

<script>
let n=10;

for (a=1; a<=n; a++)
{
document.write(a,"<br>");
}
</script>


What does the script do?
The javascript below outputs the numbers 10 to 1 using a simple for loop.
<script>
let n=10;

for (a=n; a>=1; a--)
{
document.write(a,"<br>");
}
</script>