Analysis of the use of undefined variables or values in javascript, javascript undefined

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.