Every time encounter this title brain is blank, and then all kinds of baidu all kinds of notes on the book. After a period of time to see similar problems the brain is still blank, remember notes when the paper was thrown away. Today, I will learn it again as a new knowledge.

Where does undefined come from?

Remember the simple data types in ECMAScript: Undefined, NUll, String, Number, Boolean,Symbol. Undefined is the only value of undefined (the default).

When do YOU get undefined?

The following situations:

  1. A variable is declared, butIt is not initialized, the value of this variable is undefined;
  2. rightUndefined variableThe typeof operator also returns undefined;
  3. A function that does not use a return statement to specify a return value;
  4. The function was called without passing an argument value
var str; Log (STR) //undefined console.log(STR) //undefined console.log(STR) //undefined console.log(STR) //undefinedCopy the code

Global property undefined

So let’s try it. We can explicitly initialize a variable to undefined. But in general, we don’t do this because undefined is taken by default for uninitialized values. The main purpose of undefined is to compare Pointers to empty objects.

Let STR = undefined; let STR =window. Undefined assigns undefined, which is not a value, but an attribute name -undefined is an attribute of the global object. Thus, it is equivalent to assigning the value of window.undefined to another variable STR, which is the original value of undefined.

Local property undefined

In ESMAScript, undefined is not a reserved word, which means we can use undefined as a local variable, just like any other normal variable in a local scope, without any particularity, and with any type of value.

(function() {
    var undefined = 'not is undefined';
    console.log(undefined); //"not is undefined"
    console.log(typeof undefined) // "string"
})()
Copy the code

However, the value and type of undefined have been changed, which makes our code difficult to maintain and debug.

How to judge undefined?

  1. Use either the strict equality (===) or the unequal operator (! ==) to determine whether a variable has a value.

    Standard equality is not recommended

  2. The typeof operator is the only way to detect undefined variables, otherwise an error will be reported.

(function() { var undefined = 'not is undefined'; let data; If (data === undefined){console.log('data = undefined')}else{console.log('data = undefined')}})()Copy the code

Think about what the output is.

Undefined as a local variable can be overridden, but it is risky to use the same method to determine whether data is undefined.

Void operator.

If data is undefined, find the original value undefined:

(function() { var undefined = 'not is undefined'; console.log(undefined); //"not is undefined" console.log(typeof undefined) // "string" let data; If (data === void 0){console.log('data is undefined')}else{console.log('data is undefined')}})()Copy the code
(function() { var undefined = 'not is undefined'; console.log(undefined); //"not is undefined" console.log(typeof undefined) // "string" let data; If (data === window.undefined){console.log('data is undefined')}else{console.log('data is undefined')}})()Copy the code

To sum up:

  1. Undefined is the default value for the simple data type undefined.
  2. Undefined if the variable is not initialized, or if the typyof is not defined.
  3. You can use the strict equal and unequal operators to determine whether the current variable is undefined. Use Typeof to detect undefined variables; When overwriting undefined as a local variable, you can use vido 0 or window.undefined to check whether the variable is undefined.

From this note: cavszhouyou. Top/JavaScript %…

It’s the end of the day. Next write null~~~