JavaScript rules for converting different types of data to Boolean values in an expression

Source: Internet
Author: User

There are 5 types of data in javascript: Undefined, Boolean, Object, number, String, and these types of data, when they are in an expression, The JS parser automatically converts it to a Boolean value to determine which logical branch the current condition conforms to (of course, we can also manually convert by invoking the Boolean (Var) method, which determines the code that executes the method body, which converts the values of these types to Boolean What are the rules to follow?

data type convert to True value convert to False value
undefined undefined
boolean true false
non-null is true null
number 0 and Nan
Non-empty string "" (empty string)

The above is the conversion rules JS, in the development, we must carefully analyze the possible value of variables and conversion values, if expected to get true but get false, the world will flip over slightly ^_^

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/

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.