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.