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 exampleSee 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
Randomizes the order of the values of an array, returning a new array
Example:
const tips_shuffle = ([...arr]) => { let x = arr.length; while (x) { const i = Math.floor(Math.random() * x--); [arr[x], arr[i]] = [arr[i], arr[x]]; } return arr; }; const foo = [2, 4, 6]; console.log(tips_shuffle(foo));
Output:
[4, 2, 6]
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook