Some of the more frequently used methods in JavaScript development

Source: Internet
Author: User

1. Padding string

ES7 introduced the function of full length of string complement. If a string does not have a specified length, it is complete at the head or tail.

String.prototype.padStart (MaxLength, fillstring= ")

The method is populated with fillstring before the string until the string length is maxLength.

String.prototype.padEnd (MaxLength, fillstring= ")

The method is populated with fillstring after the string , until the string length is maxLength.

To populate the string with the specified character, for example:

1) The length of the string is less than maxlength, and the specified character is populated before the string.

' Hello '. Padstart (, ' a '); // back to "Aaaaahello"

Do time countdown function, if the number is a single number, you can convert it to a string type, and then fill the front 0.

string (num). Padstart (2,0)string(// output 'down ' string (one). Padstart (2,0) // output ' one '

2) If Fillstring is omitted, a separate space string (") is used instead.

//Return "     Hello"

3) If the length of the string is greater than or equal to MaxLength, the original string is returned.

' Hello '. Padstart (5, ' a '); // return "Hello"

Some of the more frequently used methods in JavaScript development

Related Article

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.