Type conversion of JS

Source: Internet
Author: User

When a string, The number function is not used as a constructor, it can be treated as a conversion function

such as string (false), number (' 3 '), Boolean ([])

Object (3) = new number (3);

any value other than NULL and undefined has the toString () method

JS automatically detects the expression when executing the program for variable conversion.

Show Conversion variables:

You can use constructors:

String (' op '), Object (), Boolean ([]), Number (' 3 '),

Conversions between types:

Type

String

Digital

Boolean value

Object

Undefined

"Undefined"

NaN

False

Thows TypeError

Null

“”

0

False

Thows TypeError

“”

False

New string ("")

"1.2"

1.2

True

New string ("1.2")

"One"

NaN

True

New string ("one")

0

"0"

False

New Number (0)

-0

"0"

False

New number (-0)

NaN

"NaN"

False

New Number (NaN)

Infinity

"Infinity"

True

New Number (Infinity)

-infinity

"-infinity"

True

New Number (-infinity)

1

' 1 '

True

New Number (1)

{} any object

True

[] arbitrary array

0

True

[8]

"8"

8

True

[' A ']

Join

NaN

True

function

NaN

True

The object is converted to the original value:

All objects are converted to a Boolean value of true, including wrapper objects as well.

objects that are converted to strings and numeric values are called by a method to implement. The trouble is that the JS object has two different ways to convert.

The first method: thetoString () method, which returns a string that reflects the object.

({x:1,y:2}). toString ()//=>[object Object]

The array returns: Each array element is returned and added after the array element , combined into a string.

The toString of the function class returns the function definition syntax;

Date class: Returns a readable date string.

Second method:valueOf ()

This method does not define a specific function;

If the original value is returned with the original value, the object is a composite value, so the object itself is generally returned instead of returning an original value.

Special cases:

Convert object to String:

If the object has the toString method, this method is called if the method returns a raw value and converts the original value to a string.

If the object does not have a toString method, or if this method does not return a raw value, call the ValueOf method if valueOf Returns a raw value that is converted to a string.

If,value,toString cannot return the original value, an error is thrown.

Convert objects to numbers:

If the object has the valueOf method, or if this method does not return a raw value, call the ValueOf method if the valueOf Returns a raw value that is converted to a number.

If the object does not have a valueOf method, then the toString method is called if the method returns a raw value and converts the original value to a number.

If,value,toString cannot return the original value, an error is thrown.

Type conversion of JS

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.