Summary of the JavaScript Length attribute, javascriptlength
For a summary of the javascript length attribute, see the following details.
1. length in StringObject
The length attribute returns the number of characters in a string.
For example:
// Normal string var str = "abcdef"; console. log (str. length); // 6 // Array var str1 = new Array (1, 2, 3, 4); console. log (str1.length); // 4 // array and string var str2 = str1 + str; // "abcdef1, 2,3, 4" console. log (str2.length); // 13 // object and object var obj ={}; console. log (obj. length); // undefinedvar obj + = obj; // "[object Object] [object Object]" console. log (obj. length); // 30
Ii. length in Function
Length can return the number of function parameters.
Var a = function (a, B, c, d) {}; console. log (. length); // 4var B = RegExp; console. log (B. length); // The new RegExp (pattern, attributes) constructor has two parameters, so length is 2
※The length attribute of the arguments instance is the actual number of parameters that the caller passes to the function.
var a = function(){ console.log(arguments.length); };a(1,2,3); // 3a(); // 0
Note: As we all know, there is no method overload in javascript, and the arguments instance can help us simulate method overloading.
The following example shows the javascript length attribute.
Definition and usage
The length attribute returns the number of characters in a string.
Syntax
StringObject. length
Instance
In this example, we will show how to use the length attribute to return the characters in the string:
<script type="text/javascript">var txt="Hello World!"document.write(txt.length)</script>
Output:
12
The above is a summary of all the content about the javascript length attribute. I hope you will like it.