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.



JavaScript: Tips of the Day

Returns true if the given number is a power of 2, false otherwise

Example:

const tips_isPowerOfTwo = n => !!n && (n & (n - 1)) == 0;
console.log(tips_isPowerOfTwo(0)); 
console.log(tips_isPowerOfTwo(4));
console.log(tips_isPowerOfTwo(16));

Output:

false
true
true