During work, we often need to determine whether a variable/attribute is undefined. There are usually two ways to write
// Method 1 typeof age ==== 'undefined'; // method 2age === undefined
Are there any differences between the two statements? Which one should I use? Take a look at the example below
Typeof age = 'undefined'; // true
The identifier age has not been declared and the output is true.
Let's look at another example.
Age === undefined; // Error
Firebug promptAge is not defined,
This is the difference between the two, that is, it is not sure whether the age is declared or defined using method 1, but can be determined using method 2. Method 1 if the variable is not declared,CodeNo error is reported, but method 2 reports an error. It seems that method 1 has better fault tolerance and is actually a hidden bug.It is always a good habit to declare and use variables first.
In addition, method 1 is two operations, and method 2 is one operation.