Introduction to variable initialization_javascript in js

Source: Internet
Author: User
This article mainly introduces the information about variable initialization in js. If you need it, you can refer to the function that I wrote in js to remove spaces at the beginning and end and specific characters. The Code is as follows:

The Code is as follows:


Function trim (str, charlist ){
Return str. replace (new RegExp ('^ [\ s' + charlist +'] + | [\ s' + charlist + '] + $', 'G '), '');
}

The Code does not seem to have any problems, and it does not run correctly.

Today, when I searched for "note3" in the search box, I found that the content searched in the address bar is changed to "ote3", while it is normal for other letters or numbers to start, what to search and what to search in the address bar.

After debugging the code, change the code:

The Code is as follows:


Function trim (str, charlist ){
Var charlist = charlist | "";
Return str. replace (new RegExp ('^ [\ s' + charlist +'] + | [\ s' + charlist + '] + $', 'G '), '');
}

Added charlist initialization in the function. In the next search for "note3", the result is correct.

Although Javascript is a weak type language, Initialization is not required to use variables. During running, the code is automatically converted and assigned a value. However, this will cause some unexpected problems. Therefore, it is necessary to initialize all the variables used.

This is my first visit today. I hope my friends 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.