Javascript code to determine whether a variable is undefined _ javascript skills

Source: Internet
Author: User
Generally, if the variable is declared through var but not initialized, the value of the variable is undefined, and the undefined variable needs to be determined in the form of & quot; typeof variable & quot, otherwise, an error occurs, for example:

If (! MyVar01) alert ("error occurred ");

// This Code directly causes an exception because the variable myVar01 does not declare if ("undefined" = typeof myVar01) alert ("error occurred ");

// This will not cause exceptions.

Var myVar01; if (undefined = myVar01) alert ("error occurred ");

// This code runs if ("undefined" = typeof myVar01) alert ("error occurred") correctly ");

// The Code will also run correctly

Conclusion: We use the following method to ensure that if ("undefined" = typeof myVar01) alert ("error occurred") is foolproof ");

// The Code will also run correctly

Of course, judging the effectiveness of data is far more than that, and there is also a null judgment, whether the number is too large.

Practical application:
Some downlm pages are not defined, but some pages are defined, so you can use such a judgment method. If there is no definition, it will not be executed.

If ("undefined "! = Typeof downlm) {if (downlm = "soft") {document. write ('success ');}}

The test is perfect.

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.