JavaScript array length set or determine the number of array elements

 Determining the number of elements of the array may be the Array JavaScript JavaScript built-in length property, JavaScript length attribute can be used to determine the number of a string of characters, determining the number of elements can also be used within the array, the number of taken occasionally statistically equivalent It's easy to use, let's see how to use this length first, and then share an array length judgment example.


JavaScript array length judgment syntax of the number of array elements

array.length //Return the number of elements in the array array.length=number//Set the number of elements in the array


The left side of these two syntaxes is purely used to query how many elements there are in the array, and the right side is used to preset how many elements to give to the array.

JavaScript array length example
<script type="text/javascript">
var Arr = ['a','b','c','d','e'];
document.write(Arr.length);
</script>
The output of the above example: 5

This is a very simple example. First, we declare an example Arr, and give Arr five elements, including a, b, c, d, e, and then use document.write to output Arr.length On the screen, almost all major browsers support JavaScript to use the length attribute to determine the number of array elements.

Post a Comment

0 Comments