JavaScript Data type Casting

Source: Internet
Author: User
Tags type casting

First, convert to numeric type

1.number ( parameter ) converts any type to a numeric type

A. if it is a Boolean value,false is 0andtrue is 1

B. if it is a number, the conversion becomes itself. Remove the meaningless back guide 0 .

C. if Null is converted to 0

D. if undefined is converted to NaN not a number

E. if the object calls the object's valueOf () First, if valueOf () returns NaN, then the object is called the toString ()

F. if it is a string

1. If there is only a number in the string, convert to ten ( ignoring leading 0 and 0)

2. if it is a valid canonical floating-point type, convert to a floating-point value ( ignoring leading 0 and trailing 0)

3. if it is an empty string, convert to 0

4. if it is a different value, return NaN

2,parseint ( parameter 1, parameter 2) converts a string to an integer

A. if a string contains only numbers, it is converted to an integer in the form of a binary.

B. he automatically ignores the spaces in front of the string, knowing that the first non-null numeric string is found until the end of the string that resolves to the first non-numeric value.

C.returns NaN If the first character of the string is not a space, number, or-

D. parameter 1 = > string

parameter 2 = > binary

3.parsefloat () converts a string to a floating-point number

A. among the strings . only the first one is valid and the others are invalid.

B. If the string is a valid integer, he returns an integer and does not return a floating-point number.

Second, convert to string type

1.String ( parameters ) can convert any type to a string

Null and undefined: will also be converted to strings, respectively, null and undefined

Boolean type : returns true and false

Numeric type : string of itself

2.toString ()

The calling Format Object . toString ()

The function is to represent the object as a string

Array.tostring () , separated by a string

Boolean.tostring () two value true false

String.tostring () returns itself

Number.tostring ( argument ) returns the string form itself

"Numeric types can use parameters (2-32) to control the output mode "

NOTE:null and undefined do not have the toString () method

Third, convert to Boolean type

Boolean () to convert any type of value to a Boolean value

Convert to False : "", 0, NaN ,undefined, False

All the others are converted to true.


This article is from the "Programmer Technology Exchange" blog, please be sure to keep this source http://bjishu.blog.51cto.com/7481301/1670348

JavaScript Data type Casting

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.