45 useful JavaScript tips, tricks and best practices

1. Be sure to use the var keyword when assigning a value to a variable for the first time

If the variable is not declared but assigned directly, it will be regarded as a new global variable by default. Avoid using global variables as much as possible.

2. Use === to replace ==

The == and != operators will automatically convert data types when needed. But === and !== will not, they will compare values ​​and data types at the same time, which also makes them faster than == and !=.

[10] === 10    // is false
[10]  == 10    // is true
'10' == 10     // is true
'10' === 10    // is false
 []   == 0     // is true
 [] ===  0     // is false
3. The logical results of underfined, null, 0, false, NaN, and empty strings are all false
4. Use a semicolon at the end of the line

Guess you like

Origin blog.csdn.net/weixin_43131046/article/details/114370697
Recommended