Select elements using negative values: - Javascript Array

Javascript examples for Array:slice

Description

Select elements using negative values:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>

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

<script>
function myFunction() {/*from   www .  ja  v  a 2s  .c o  m*/
    var fruits = ["A","B","C","D","E"];
    var myBest = fruits.slice(-3, -1);
    document.getElementById("demo").innerHTML = myBest;
}
</script>

</body>
</html>

Related Tutorials