JavaScript length property

 The JavaScript length property can be used to determine the number of elements, such as judging how many characters are in the string, judging how many array elements there are in the array, etc. The length property is a built-in function of JavaScript and does not require additional installation or plug-in. Call it at any time, and almost all major browsers support the JavaScript length property.


JavaScript length basic syntax

string-or-array.length


The syntax is quite simple, just add .length to the string String or Array to be judged, and then use two examples to introduce.

Example of JavaScript length determining the number of characters in a string
<script type="text/javascript">
 var TestStr = '0123456789';
 document.write(TestStr.length);
</script>
The above output result is: The 10

example first prepares a string TestStr at the beginning, and gives the string a value of 0-9. Through Test.length, it can be judged that there are 10 elements in total.

Example of JavaScript length determining the number of elements in an array
<script type="text/javascript">
 var Arr = ['Father','Mother','Brother'];
 document.write(Arr.length);
</script>
The above output result is: 3

We have prepared an array Arr in advance, which contains three array elements Father, Mother and Brother. Through Arr.length, we can judge that there are three array elements in total. Document.write means only outputting the result It's just on the screen.

Post a Comment

0 Comments