Preface:

Recently sorted out some of the js easy to be ignored in the small knowledge points, here to share, more understanding of some convenient for us to read the source code in the future, improve js skills.

1. Variables created by var cannot be deleted

var a = 1; delete a; console.log(a); / / output 1Copy the code

2. You can delete variables that are not created using var

a = 1; delete a; console.log(a); // Uncaught ReferenceError: A is not definedCopy the code

3. Function declarations take precedence over function expressions

var test = function(){ return 1; }; function test(){ return 2; } console.log(test()) // Output: 1Copy the code

4. Function declarations in code blocks or if statements are not promoted

A (){function a(){console.log(1)}} Uncaught TypeError: A is not a functionCopy the code

5. Logical operators

let test = res.data.obj || {}; // If res.data.obj is false, assign test {} a && b(); Equivalent to if(a){b(); } // If a is true, execute b;Copy the code

6. Change the number type to String

2. ToString () // Error 2.. ToString ()//'2' //js does not distinguish between integers and floating-point numbers. The first point encountered is treated as a point in a floating-point numberCopy the code

7. Freeze objects

Let obj = {a:1, b:1} object.freeze (obj) // Freeze Object obj. Console. log(obj.a) // Attributes cannot be added, modified, or deleted after the object is frozenCopy the code

8. The 0 before the number can be omitted, and the 0 after the decimal point can be omitted (of course, it is not recommended in normal cases).

Let a = 0.4 is equivalent to let a =.4; Let b = 3.0; Equivalent to let b = 3.Copy the code

9. ToFixed returns a string

Let a = 3.1415; a.toFixed(2); / / '3.14'Copy the code

10. 0.1 + 0.2! =0.3

The console. The log (0.1 + 0.2! =0.3) // output //true: javascript follows IEEE 754 specification and performs binary conversion during calculation. Individual bits will be lost during conversion, resulting in the sum of converted values not equal to 0.3Copy the code

11. Undefined vs. null

1. Use undefined == null to return true; 2. Undefined is the default value if the variable is not defined. For example, var a = 3; There are two steps in compilation execution. 1.var a == undefined; 2.a = 3; Null has no value at this point.Copy the code

12. Void operator

In individual components, undefined is usually replaced by void 0 to prevent undefined from being redefined. Void 0 === undefined output: true void 1 === undefined output trueCopy the code