Summary of the JavaScript Length attribute, javascriptlength

Source: Internet
Author: User

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.

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.