Recently, a friend of mine was looking for a job. He was often asked about his understanding of undefined in an interview, but his answer was not comprehensive. After careful consideration, we may sometimes ignore these very basic details, so today I will make a detailed summary of undefined
1. Undefined is a primitive data type and a primitive value.
2, undefined is a property of the global object window.undefined.
3, writable: false
window.undefined = 123;
console.log( undefined );
// undefined
Copy the code
4. No additional information can be configured, exercising any control system: false
delete window.undefined;
console.log( undefined );
// undefined
Copy the code
Enumerable: False
for( var k in window) {
if( k === undefined) { cosole.log( k ); }}// No output
Copy the code
6. Cannot be redefined
Object.definProperty(window.'undefined', {
writable: true.enumberable: true.configurable: true
})
// The result is incorrect
Copy the code
7. The system will automatically assign the value of undefined to the variable
var a;
console.log( a );
// undefined
Copy the code
8. If the function does not return a value, the system will automatically return undefined
function a () {
console.warn('123')}console.log( a() );
// undefined
Copy the code
9, global: window.undefined cannot be written, but local scope can be reassigned.
var undefined = 1; // assign window.undefined
console.log( undefined );
// undefined; undefined
function test() {
var undefined = 1;
console.log( undefined )
}
test();
// 1 The reason is that undefined is not a js keyword.
Copy the code
Undefined is not a js keyword, so you can use undefined as a variable name in a local scope.
- How to tell if
undefined
To check whether the value is undefined, use congruence ===
var a;
if(a===nudefined){
console.log(true);
}else{
console.log(false)}// true
Copy the code
Why not use == in case of error
var a=null;
if(a==nudefined){
console.log(true);
}else{
console.log(false)}// The result is also true
Copy the code
To prevent this, it is best to use congruence
Since undefined can be reassigned as a variable name in a local scope, it is best to use void(0) or window.undefined instead. Void (0) returns undefined, which is more efficient than window.undefined.
11, undefined variable, typeof will output undefined, will not report error
typeof
var a ;
console.log(typeof a) ; //undefined;
console.log(typeof b); //undefined
Copy the code
Because typeof is processed. Therefore, undefined variables will not fail, but return undefined
If console.log(b) is printed separately, an error is reported.
VM402:1 Uncaught ReferenceError: b is not defined at <anonymous>:1:13
A. in B. in C. in D. in
var a;
if('a' in window) {console.log(true)}else {
console.log(false)}Copy the code
Void keyword
Void (0) : evaluates 0 to return undefined
Void returns undefined
The use of the void
Assign a value undefined
var a,b,c;
a= void(b=1,c=2);
console.log(a,b,c)
//undefined 1 2
Copy the code
console.log( void(0) === window.undefined )
;
The result is true;
Example:
function test(){
var undefined =1;
console.log(undefined); / / 1
console.log(void(0)); //undefined
console.log(undefined= = =void(0)); //false
console.log(window.undefined===void(0)); //true
}
test();
Copy the code
Void (0); void(1); void(100); or window.undefined
Void (0) is more efficient than window.undefined
Now, the code is relatively regular, so void(0) is less common. But the underlying code will still be useful
Finally, I want you to code the specification