JS data type conversion and data validity judgment

Source: Internet
Author: User

1. Data type Conversion

parseint () Example:

parseint ("1234blue");//return 1234

parseint ("22.5");//return 22 for integers, the decimal point is an invalid character

parseint ("Blue");//return NaN

Parsefloat () Example:

Parsefloat ("1234blue");//return 1234.0

Parsefloat ("22.34.5");//return 22.34

Parsefloat ("0908");//return 908

Parsefloat ("Blue");//return NaN

Boolean () Example:

Boolean ("");//return false

Boolean ("Hi");//return true

Boolean (+);//return true

Boolean (null);//return false

Boolean (0);//return false

Boolean (New Object ());//return true

Number () Example:

Number (false);//return 0

Number (true);//return 1

Number (undefined);//return NaN

Number (null);//return 0

2. Data validity judgment (e.g. null, empty, etc.)

(1). The NaN property is a special value that represents a non-numeric value, indicating that a value is not a number.

The IsNaN () global function is often used to determine whether a value is a NaN value.

Methods parseint () and parsefloat () return this value when the specified string cannot be resolved.

    var exp = number (undefined); Return NaN
if (IsNaN (TMP))
{
Alert ("NaN");
}

(2). Undefined judgment: Use the typeof () method.

    var exp = undefined;
if (typeof (exp) = = undefined)
{
Alert ("undefined");
}

(3). Null judgment: Use the typeof () method.

    var exp = null;
if (!exp && typeof (exp)! = "undefined")
{
Alert ("is null");
}

JS data type conversion and data validity judgment

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.