English [leŋθ] American [leŋθ]

n.Length, long; length of time; (speech) sound length; paragraph, section

Plural: lengths

javascriptlengthproperty syntax

How to use the length attribute?

Length is a property in JavaScript. The length property can be used to set or return the number of elements in an array.

Function: Can set or return the number of elements in the array.

Syntax: arrayObject.length

Description: The length property of an array is always lower than the last element defined in the array Mark it by 1. For regular arrays with consecutive elements starting with element 0, the length property declares the number of elements in the array. The length property of an array is initialized when the array is created using the Array() constructor. When a new element is added to the array, the value of length is updated if necessary. Set the length property to change the size of the array. If set to a value smaller than its current value, the array will be truncated and its trailing elements will be lost. If the set value is greater than its current value, the array will grow and new elements will be added to the end of the array, with their value being undefined.

Comments: None

javascriptlengthproperty example

<html>
<body>

<script type="text/javascript">

var arr = new Array(3)
arr[0] = "西门"
arr[1] = "无忌"
arr[2] = "慕容复"

document.write("Original length: " + arr.length)
document.write("<br />")

arr.length=5
document.write("New length: " + arr.length)

</script>

</body>
</html>

Run instance »

Click the "Run instance" button to view the online instance