JavaScript Array keys() Method

You are Here:

JavaScript Array keys() Method

The keys() method returns a new Array Iterator object that contains the keys for each index in the array.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myFruit = ["Apple", "Banana", "Jackfruit"]; var iterator = myFruit.keys(); for(let key of iterator) document.write(key + "<br>"); </script> </body> </html>

Syntax

Array.keys()

keys() for Associative Array

The keys() method doesn't work with associative array

HTML Online Editor
<!DOCTYPE html> <html> <body> <script> var myFruit = []; myFruit["red"] = "Apple"; myFruit["yellow"] = "Banana"; myFruit["green"] = "Jackfruit"; var iterator = myFruit.keys(); for(let key of iterator) document.write(key + "<br>"); </script> </body> </html>

keys() for Map

Use Map instead of associative array.

HTML Online Editor
<!DOCTYPE html> <html lang="en-US"> <body> <script> var myFruit = new Map([ ['red', 'Banana'], ['yellow', 'Guest'], ['green', 'Jackfruit'], ]); var iterator = myFruit.keys(); for(let key of iterator) document.write(key +"<br>"); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author