Summary of JavaScript Length attributes
- 2020-09-16 07:22:22
- OfStack
For a summary of the javascript length attributes, see the following for a detailed explanation.
1. length in StringObject
The length attribute is the number of characters that return a string.
Such as:
// Ordinary string
var str = "abcdef";
console.log(str.length); // 6
// An array of
var str1 = new Array(1,2,3,4);
console.log(str1.length); // 4
// Arrays and strings
var str2 = str1 + str; // "abcdef1,2,3,4"
console.log(str2.length); // 13
// Objects and objects
var obj = {};
console.log(obj.length); // undefined
var obj += obj; // "[object Object][object Object]"
console.log(obj.length); // 30
2. length in Function
length returns the number of arguments to function.
var a = function(a,b,c,d){};
console.log(a.length); // 4
var b = RegExp;
console.log(b.length); //new RegExp(pattern, attributes) There are two parameters in the constructor , so length for 2
※ The length attribute of the arguments instance returns the actual number of arguments passed to the function by the caller.
var a = function(){
console.log(arguments.length);
};
a(1,2,3); // 3
a(); // 0
Note: As we all know, there is no method overloading in javascript, and an instance of arguments just happens to help us simulate method overloading.
Here is an example of the javascript length attribute
Definition and usage
The length attribute returns the number of characters in a string.
grammar
stringObject.length
The instance
In this example, we will show how to use the length attribute to return the number of characters in a string:
<script type="text/javascript">
var txt="Hello World!"
document.write(txt.length)
</script>
Output:
12
That is the summary of javascript length attribute. I hope you enjoy it.