JavaScript Array length Property
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 1 above
- Edge 12 and above
- Firefox 1 and above
- Internet Explorer 4 and above
- Opera 4 and above
- Safari 1 and above