How to use For loop in JavaScript
In this article I am going to explain about for loop in JavaScript.
JavaScript For loop
We use loop when we want to execute any code over and over again till specified number of time.
- For loop execute code specified number of time
- We use for loop when we know code how many time should be run.
- In For loop firstly initialized variable, then condition and in last increment.
For loop syntax
for(Variable= setvalue; Variable> condition; Variable+ increment)
{
//our code that that should be run
}
|
In this example it will display 1 to 10 natural number
<html>
<head>
<script type="text/javascript">
function MyFunction() {
for (i = 0; i < 11; i++) {
document.write(i + ",");
}
}
</script>
</head>
<body>
<button onclick=" MyFunction()">
Click</button>
</body>
</html>
|
Output

Further Readings
You may also want to read these related articles: here
Ask Your Question
Got a programming related question? You may want to post your question here