Use for Loop to calculate total in JavaScript

Description

The following code shows how to use for Loop to calculate total.

Example


<!-- w  w w  . j  a v  a 2 s.c o  m-->

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var i, total
total = 0
for (i = 1; i < 5; i++) {
total += i
document.write("Loop iteration " + i + " (Cumulative total = "  + total + ")<BR>")
}

</script>
</head>
<body>
</body>
</html>

Click to view the demo

The code above generates the following result.

Use for Loop to calculate total in JavaScript