45 Javascript tips and 45 javascript tips

Source: Internet
Author: User

45 Javascript tips and 45 javascript tips

Original article: 45 Useful JavaScript Tips, Tricks and Best Practices

45 useful JavaScript tips and best practices

Translator: dwqs

In this article, I will share some common JavaScript skills, tips, and best practices. JavaScript developers should know whether they are using browsers/engines or server-Side (SSJS -- Service Side JavaScript) JavaScript interpreters.

It should be noted that all the code snippets in the article have been tested on the latest Google Chrome (version 30), which uses the V8 JavaScript Engine (V8 3.20.17.15 ).

1. Do not forget the var keyword when assigning values to variables.

If you assign a value to an undefined variable, the variable automatically becomes a global variable.

2. Use = instead of =

= (Or! =) The operator will automatically convert the variable type as needed, while ===( or! Type conversion is not performed. When comparing values and types, consider using =

[10] === 10    // is false[10]  == 10    // is true'10' == 10     // is true'10' === 10    // is false []   == 0     // is true [] ===  0     // is false '' == false   // is true but true == "a" is false '' ===   false // is false

 

3. undefined, null, 0, false, NaN, ''(null String) are regarded as false.

4. Use a semicolon at the end of a row

Using semicolons at the end of a row is a good practice. If you forget it, you will not receive a warning because the JavaScript interpreter inserts a semicolon in most cases.

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.