"Knowledge Points" 11 interesting facts in JavaScript

1 NaN is a number type

NaN is a number type. Moreover, NaN is not equal to itself. In fact, NaN is not equal to anything. To verify whether a variable is NaN, you can use the isNaN() method to judge.

> typeof(NaN)"number"> NaN === NaNfalse


2 null is an object

null is an object. It sounds strange! Correct? But this is true.

> typeof(null)"object"

In this case, null means no value. Therefore, null should not be an instance of Object.

> null instanceof Objectfalse

3 undefined can be defined

undefined is not a reserved keyword in JS, you can specify a value for it without reporting an error. If a variable is declared without a value, the default is undefined

> var some_var;undefined> some_var == undefinedtrue> undefined = 'i am undefined'


4 0.1 + 0.2 does not equal to 0.3

In JavaScript, 0.1 +0.2 == 0.3 returns false. The fact is that javascript stores floating point numbers as binary.

> 0.1 + 0.20.30000000000000004> 0.1 + 0.2 == 0.3false


5 Math.max() is smaller than Math.min()

The fact that Math.max()> Math.min() returns false looks wrong, but it is actually correct.

If no parameters are passed to min() or max(), then it will return the following values.

> Math.max()
-Infinity> Math.min()Infinity


6 018 - 045 = -19

In JavaScript, the prefix 0 will convert any number to octal. However, 8 is not used in the octal system, and any number containing 8 will be silently converted to a regular decimal number.

> 018 - 045-19

Therefore, 018-019 is actually equivalent to the decimal expression 18-37, because 045 is octal, but 018 is decimal.


7 Functions can be executed by themselves

Just create a function and call it immediately when calling other functions, and use the () syntax

> (function()  { console.log('I am self executing');  })();
I am self executing


8 The position of parentheses

When there is nothing after the `return` statement, it returns nothing. In fact, JS adds a `;` after `return`.

> function foo() {   return
   {      foo: 'bar'
   }
}
> foo(); 
undefined> function foo() {   return {      foo: 'bar'
   }
}
> foo(); 
{foo: "bar"}


9 No integer data type

In JS, there is no int (integer) data type. All numbers are of Number type. In fact, it stores the floating point value of an int number in memory.


10 sort() function automatic type conversion

The sort() function automatically converts the value to a string, which can cause strange things to happen.

> [1,5,20,10].sort()
(4) [1, 10, 20, 5]

However, it can be solved by comparing:

> [1,5,20,10].sort(function(a, b){return a - b});
(4) [1, 5, 10, 20]


11 Sum of array and object

> !+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+!![]9> {} + []0> [] + {}"[object Object]"> [] + []""> {} + {}"[object Object][object Object]"> {} + [] == [] + {}true
> !+[]+!![]+!![]+!![]+!![]+!![]+!![]+!![]+!![]9> {} + []0> [] + {}"[object Object]"> [] + []""> {} + {}"[object Object][object Object]"> {} + [] == [] + {}true


Guess you like

Origin blog.51cto.com/15128443/2675851