Determine if the JavaScript variable is empty

Source: Internet
Author: User

In the case of indeterminate variable types, determine if the JavaScript variable is empty and do not use:

if (TMP) {}

The problem with this judgment is that, for example, when the value of TMP is number 0 o'clock, false is also returned.

So try to use:

if (tmp! = null) {}

The following is the result of the IF (TMP) {} judgment:

if (TMP) if (tmp! = NULL)

//false//false when TMP is undefined

var tmp; False//false

var tmp = 0; False//true

var tmp = 10; True//true

var tmp = ""; False//true

var tmp = "ddd"; True//true

var tmp = 0.0; False//true

var tmp = new String (); True//true

Conclusion: To determine if tmp is undefined or null can be used if (tmp! = NULL), cannot use if (TMP)

Determine if the JavaScript variable is empty

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.