Loop through the indices of an array, in descending order (negative increment): - Javascript Statement

Javascript examples for Statement:for

Description

Loop through the indices of an array, in descending order (negative increment):

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<p>Click the button to loop through the indices of an array, in descending order.</p>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*  w ww .  j av  a 2  s . co  m*/
    var cars = ["a","b","c","d","e"];
    var text = "";
    var i;
    for (i = cars.length - 1; i >= 0; i--) {
        text += cars[i] + "<br>";
    }

    document.getElementById("demo").innerHTML = text;
}
</script>

</body>
</html>

Related Tutorials