Home »
JavaScript
Array length property with example in JavaScript
JavaScript array length property: Here, we are going to learn about the length property of array in JavaScript.
Submitted by IncludeHelp, on February 27, 2019
JavaScript length property
length property is used to get and set the length of an array in JavaScript.
Syntax to get the length of an array:
array_name.length;
Syntax to set the length of an array:
array_name.length = value;
//Here, value is the total number elements
//(length) to set to an array.
Sample Input/Output
Input:
var arr1 = [10, 20, 30, 40, 50];
Function call:
arr1.length;
Output:
5
JavaScript Code to get/set the length of an array
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
var arr1 = [10, 20, 30, 40, 50];
var arr2 = ["Amit", "Ankur", "Akash"];
var arr3 = [];
//getting length
document.write("length of arr1: " + arr1.length + "<br>");
document.write("length of arr2: " + arr2.length + "<br>");
document.write("length of arr3: " + arr3.length + "<br>");
//setting a length
arr3.length = 10;
document.write("length of arr3: " + arr3.length + "<br>");
</script>
</body>
</html>
Output
length of arr1: 5
length of arr2: 3
length of arr3: 0
length of arr3: 10
JavaScript Array Object Methods »