How to check a isn't defined variable in JavaScript

Source: Internet
Author: User

How do you check for an undefined variable in javascript?

In JavaScript null was an object. There ' s another value for things that don ' t exist, undefined. The DOM returns null for almost all cases where it fails to find some structure in the document, and in JavaScript itself Undefined is the value used.

Second, no, they is not directly equivalent. If you really want to check for null, do:

if (null = = Yourvar)//with castingif (null = = = Yourvar)//Without casting

If you want to check if a variable exist

if (typeof yourvar! = ' undefined ')//any Scopeif (window[' varname ')! = undefined)//Global Scopeif (window[' varname ')! = void 0)//old browsers

If you know the variable exists but don ' t know if there ' s any value stored in it:

if (undefined! = Yourvar) if (void 0! = Yourvar)//For older browsers

If you want to know if a member exists independent of whether it had been assigned a value or not:

if (' MemberName ' in object)//With Inheritanceif (Object.hasownproperty (' membername '))//Without inheritance

If you want to know whether a variable autocasts to true:

if (variablename)

Source

Original: Http://stackoverflow.com/questions/858181/how-to-check-a-not-defined-variable-in-javascript

How to check a isn't defined variable in JavaScript

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.