It's interesting to learn about some of the programming quirks of JavaScript, and it's worth summarizing:
1. Unknown variable name Create global variable
in our usual programming of JavaScript, some people are not very formal, when defining variables, the direct definition of "variable name = value" is not preceded by "Var", JavaScript automatically defines it as a global variable view
But in ECMAScript5 's rigorous mode get a warning:
For example: function f () {' Use strict '; foo = 123} f () Referenceerror:foo are not defined
2. Two null values of underfined and NULL
For "null" or "null reference", most programming languages have only one value. For example, in Java null
, 2 values (undefined and null) are given in JavaScript
underfined:
<script type= "Text/javascript" > var s; Console.log (s); </script>
Results:
Similarly: If there is no value in the operation of the value parameter, a undefined is automatically assigned.
<script type= "Text/javascript" > functionreturn x} Console.log (x); </script>
Running results in Firefox:
Running results in IE: Khan! Firefox at least to a undefined,ie directly on the judgment is undefined, this mechanism is too lazy ...
null: Is used by the developer to explicitly indicate that a value is missing
During the development process, we may need to pass this parameter without a value, and set the parameter equal to NULL for this scenario.
School check: Does a variable have a value?
<script type= "Text/javascript" > // var X; if (x) {Console.log ( ' has value '
);
else {Console.log ( ' no value '
); } </script>
If you want to know the variable x
&NBSP; undefined
&NBSP; null
. Fortunately, the two values are false. Therefore, it is possible to test whether the two items are true at the same time using only one judgement, &NBSP; false
, &NBSP; -0
, &NBSP; +0
, &NBSP; NaN
and "as null values. If this is not what you want, then you cannot use the school check method above. There are 2 ways to choose
Some people like to use "! =" for parameter checking
JavaScript Quirks Analysis