Saturday, December 28, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Array length Property

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 the array.

array.length

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

Below is an example of the Array length property.

Example: This example returns the length of the array. 

Javascript




function array() {
    let colors = ["green", "blue", "red",
        "yellow", "black", "white"];
 
    console.log(colors.length);
}
array();


Output

6

More example codes for the above property are as follows: 

Example 1: 

Javascript




let arr = new Array("Geeks", "for", "Geeks");
console.log("arr.length:" + arr.length);


Output:

arr.length:3

Example 2: 

Javascript




let arr = new Array(5, 10, 15);
console.log("arr.length:" + arr.length);


Output:

arr.length:3

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

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

  • Google Chrome 1 above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 4 and above
  • Safari 1 and above

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript

RELATED ARTICLES

Most Popular

Recent Comments