JavaScript array.keys()


The array.keys() method of JavaScript is used to return an Array Iterator object with the keys of an array.

The syntax is as follows −

array.keys()

Let us now implement the array.keys() method in JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
   <h2>Car Variants</h2>
   <p id="test"></p>
   <script>
      var arrStud = ["Crossover", "Convertible", "Hatchback", "SUV"];
      var res = arrStud.keys();
      for (val of res) {
         document.getElementById("test").innerHTML += val + "<br>";
      }
   </script>
</body>
</html>

Output

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
   <h2>Ranking Points</h2>
   <p>Click the button to display only the keys</p>
   <button onclick="display()">Result</button>
   <p id="test"></p>
   <script>
      var pointsArr = [10, 20, 30, 40, 50, 60, 70, 80, 90, 1000];
      document.getElementById("test").innerHTML = pointsArr;
      function display() {
         var res = pointsArr.keys();
         for (val of res) {
            document.getElementById("test").innerHTML += val + "<br>";
         }
      }
   </script>
</body>
</html>

Output

Above, click the “Result” button −

Updated on: 17-Dec-2019

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements