Skip to content
Related Articles

Related Articles

JavaScript Array length Property
  • Last Updated : 24 Jun, 2020

Below is the example of the Array length property.

  • Example: This example returns the length of array.




    <!DOCTYPE html>
    <html>
    <head>
        <title>
            JavaScript Array length Property
        </title>
        <style>
            body {
                text-align: center;
            }
            h1 {
                color: green;
            }
        </style>
    </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>         

    
    

  • Output:

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.

More example codes for the above property are as follows:
Program 1:






<script type = "text/javascript"
    var arr = new Array( "Geeks", "for", "Geeks"); 
    document.write("arr.length:" + arr.length); 
</script>     


Output:

arr.length:3

Program 1:




<script type = "text/javascript"
    var arr = new Array( 5, 10, 15); 
    document.write("arr.length:" + arr.length); 
</script>     


Output:

arr.length:3

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

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

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :