Summary _javascript tips for JavaScript Length properties

Source: Internet
Author: User

For a summary of the JavaScript length attribute, please see the following details.

Length in the Stringobject

The Length property is the number of characters that return a string.

For example:

Ordinary string
var str = "abcdef";
Console.log (str.length); 6
//array
var str1 = new Array (1,2,3,4);
Console.log (str1.length); 4
//array with string
var str2 = str1 + str;//"abcdef1,2,3,4"
Console.log (str2.length);///
object and Object
V Ar obj = {};
Console.log (obj.length); Undefined
var obj + = obj;//"[Object Object][object Object]"
console.log (obj.length);//30

Length in function

Length can return the number of parameters for a function.

var a = function (a,b,c,d) {};
Console.log (a.length); 4
var b = RegExp;
Console.log (b.length); The new REGEXP (pattern, attributes) construction method has two parameters, so length is 2

The length property of the ※arguments instance returns the number of actual arguments passed to the function by the calling program.

var a = function () {
  console.log (arguments.length); 
};
A (1,2,3); 3
A ();//0

Note: As we all know, there are no overloads of methods in JavaScript, and arguments instances can help us simulate the overloads of methods.

The following is an example to introduce the JavaScript length property

Definitions and usage

The Length property can return the number of characters in the string.

Grammar

Stringobject.length

Instance

In this case, we'll show you how to use the Length property to return the number of characters in a string:

<script type= "Text/javascript" >
var txt= "Hello world!"
document.write (txt.length)
</script>

Output:

12

The above is about the JavaScript Length property summary of the entire content, I hope you like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.