The pure undefined method in JavaScript, javascriptundefined
1. Why get undefined?
Because undefined is not a reserved word in javascript, it can be assigned as a variable. If undefined is used to detect a variable in the future, the value of undefined is inaccurate;
Example:
Var undefined = 10; function sum (a, B) {if (a === undefined | B === undefined) {console. log ("Incorrect Parameter");} 18101130357 return a + B ;}
Sum ()-> If the parameter is correct, the console outputs "parameter error ";
At this time, in order to be compatible with all browsers, we need to obtain a pure undefinde
2. How to obtain pure undefined?
1) void (0 ):
The ECMAScript 262 specification is described as follows:
The void OperatorThe production UnaryExpression : void UnaryExpression is evaluated as follows:Let expr be the result of evaluating UnaryExpression.Call GetValue(expr).Return undefined.
In short, remember that no matter what the expression after void is, the void operator will return undefined.
2) input a parameter that is assigned a value.
[Case]
Function (_ undefined) {// The value of _ undefined is not assigned to the function body. The value of _ undefined is undefined. You can use _ undefined in this function}
3) unassigned variables
For example, var num // The truth is the same as 2 ).
Do not get bored with familiar things, make progress every day; Do not fear unfamiliar things, learn a little every day;
PS: js determines the undefined type
If (reValue = undefined) {alert ("undefined");} found that the judgment could not be found, and finally found the information to use the typeof method: if (typeof (reValue) = "undefined") {alert ("undefined ");}
Typeof returns a string, which may be "number", "string", "boolean", "object", "function", or "undefined"
Articles you may be interested in:
- Detailed analysis of differences between js and undefined types, undefined and null
- Js checks whether the parameter (String, Array, Object) is undefined or the value is null.
- Js judges the undefined variable type using typeof
- Summary of differences between null, NaN, and undefined in Javascript
- Js null, undefined, string Summary
- Differences between undefined and null in javascript
- JavaScript code used to determine undefined
- Difference between JavaScript null and undefined
- Analysis of null and undefined in JavaScript
- JavaScript Undefined, difference between the Null type and the NaN Value
- Differences between undefined and "undefined" In JScript
- Distinguish undefined, null, "", 0, and false in JS