Analysis of the use of undefined variables or values in javascript, javascript undefined
This article describes how to use undefined variables or values in javascript. We will share this with you for your reference. The details are as follows:
Undefined values cannot be used in javascript, except in the following situations:
1. In the assignment statement:
a=9;alert(a) //9
If the variable to be assigned a value is not defined in the assignment statement, it is first defined and then assigned a value. In addition, the value assignment statement is executed from the right to the left if no error is reported for a = B = c = 8.
2. in the for in statement:
for(key in {name:'goofy'}){ alert(key) //"name"}alert(key) //"name"
In the for in statement, if the variable on the left of in is not defined, it is first defined.
3. After the typeof OPERATOR:
alert(typeof a) //'undefined'alert(a) //Uncaught ReferenceError: a is not defined
The typeof operator can be associated with an undefined value, but does not define it.
4. object attributes:
var o={name:'goofy'}alert(o.name) // 'goofy'o[age]=24; // Uncaught ReferenceError: age is not definedalert(o.age)
When defining object attributes, you can use unspecified values in the form of json direct data, but an error will be reported if the following format is used.
5. function parameters:
function fn(a,b){ alert(a) //4 alert(b) //'undefined'}fn(4)
Parameters are automatically defined during function execution. Therefore, function parameters can be directly used in the function body. If this parameter is not passed during real-time method calls, no error is reported.