w3resource

JavaScript length Property: Array Object

Description

The length property returns number elements of an array. The value of the length property is an integer and less than 232. The length property is also used to truncate an array. If the length property is set with a number which is greater than the original length then new elements are added to the end of the array. If the length property is set with a number which is less than the original length then excess elements at the end of the array are lost.

Version

Implemented in JavaScript 1.1

Syntax

array.length

Example:

In the following web document length property is used to get the number of elements of a given array.

HTML Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8" />
<title>JavaScript Array object-length Property example</title>
<style type="text/css">
h1 {color:red}
</style>
</head>
<body>
<h1>JavaScript Array Object : length Property</h1>
<script src="array-length-example1.js">
</script>
</body>
</html>

JS Code

var myarray = new Array("Orange", "Apple", "Banana", "Cherry", "Mango");
var newParagraph = document.createElement("p");
var newText = document.createTextNode("The length of the array is : "+myarray.length); 
newParagraph.appendChild(newText); 
document.body.appendChild(newParagraph);

View the example in the browser

Practice the example above online

JavaScript Array object-length Property example

See also:

JavaScript input Property: Array Object

Previous: JavaScript index Property: Array Object
Next: JavaScript prototype Property: Array Object

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/javascript/object-property-method/array-length.php