Analysis of Using Undefined Variables or Values in javascript

  • 2021-07-04 17:52:29
  • OfStack

This article illustrates the use of undefined variables or values in javascript. Share it for your reference, as follows:

Undefined values cannot be used in javascript, except in the following cases:

1. In the assignment statement:


a=9;
alert(a) //9

If the variable that needs to be assigned in the assignment statement is not defined, it will be defined first and then assigned. In addition, it can be seen from a=b=c=8 that the assignment statement is executed from right to left.

2. In the for in statement:


for(key in {name:'goofy'}){
    alert(key) //"name"
}
alert(key) //"name"

The variables to the left of in in the for in statement will be defined first if they are not defined

3. After the typeof operator:


alert(typeof a) //'undefined'
alert(a) //Uncaught ReferenceError: a is not defined

The typeof operator can follow an undefined value, but does not actively define it

4. Object properties:


var o={name:'goofy'}
alert(o.name) // 'goofy'
o[age]=24; // Uncaught ReferenceError: age is not defined
alert(o.age)

When defining an object attribute, if it is in the form of json direct quantity, you can use an undefined value, but if it is in the form of subscript, an error will be reported

5. Parameters of function:


function fn(a,b){
    alert(a) //4
    alert(b) //'undefined'
}
fn(4)

The function will actively define parameters when executing, so the function parameters can be directly used in the function body, and this parameter will not be passed and will not report errors when calling the method

For more readers interested in JavaScript related contents, please check the topics of this site: "Summary of json Operation Skills in JavaScript", "Summary of JavaScript Switching Special Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Error and Debugging Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills" and "Summary of JavaScript Mathematical Operation Usage"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: