JavaScript | Array length Property

The array length property in JavaScript is used to set or return the number of elements in an array.

Syntax:

  • It is used to set the array length.
    array.length = number
  • It returns the length of array.
    array.length

Return Value: It returns a numerical value, denoting the number of elements in the array object.

Example: This example returns the length of array.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript Array length Property
    </title>
</head>
  
<body>
    <h1>GeeksforGeeks</h1>
      
    <h2>
        JavaScript Array length Property
    </h2>
      
    <button onclick="array()">
        Click Here!
    </button>
      
    <p id="gfg"></p>
      
    <!-- Script to return  the length of array -->
    <script>
        function array() {
        var colors = ["green", "blue", "red", 
                      "yellow", "black", "white" ];
                        
            document.getElementById("gfg").innerHTML
                    = colors.length;
        }
        </script>
</body>
  
</html>                    

chevron_right


Output:
Before clicking the button:

After clicking the button:

Supported Browsers: The browser supported by JavaScript array length property are listed below:

  • Google Chrome
  • Firefox
  • Internet Explorer
  • Opera
  • Safari


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.