Is there a standard function to check for null, undefined, or blank variables in JavaScript?

Source: Internet
Author: User

You can just check if the variable have a truthy value or not. That means

if( value ) {}

Would evaluate to true if are not value :

    • Null
    • Undefined
    • NaN
    • Empty string ("")
    • 0
    • False

The above list represents all possible falsy values in Ecma-/javascript. Find it in the Specificationat ToBoolean .

Furthermore, if you don't know whether a variable exists (that means, if it is declared) you should CH Eck with the typeof operator. For instance

if( typeof foo !== ‘undefined‘ ) {    // foo could get resolved and it‘s defined}

If you can being sure that a variable are declared at least and you should directly check if it have a truthy value like sh Own above.

Further read:http://typeofnan.blogspot.com/2011/01/typeof-is-fast.html

http://stackoverflow.com/questions/5515310/ is-there-a-standard-function-to-check-for-null-undefined-or-blank-variables-in/5515349#5515349

Is there a standard function to check for null, undefined, or blank variables 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.