JavaScript type conversion

Source: Internet
Author: User

JavaScript type conversions:

Value Convert to: String Digital Boolean value Object
Undefined "Undefined" NaN False Throws TypeError
Null "NULL" 0 False Throws TypeError
True "True" 1 New Boolean (True)
False "False" 0 New Boolean (FALSE)
"" Empty string 0 False New String ("")
"1.2" (non-empty, digital) 1.2 True New String ("1.2")
"One" (non-empty, non-numeric) 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 (infinity, not 0) "1" True New Number (1)
{} (Any object) True
[] (any array) “” 0 True
[9] (1 Digital objects) "9" 9 True
[' A '] (other arrays) Using the Join () method NaN True
function () {} (any function) NaN True

Conversions and equality

null==undefined//These two values are considered equal

"0" = 0//string converted to a number before comparison

0==false//Before comparison the Boolean value is converted to a number

"0" ==false//Before comparison string and Boolean values are converted to numbers

the "= =" equals operator casts a type when judging whether two values are equal, but converting one value to another does not mean that two values are equal. the "= = =" Identity operator does not make any type conversions when judging equality.

Display Type conversions

Display type conversions are performed using the Boolean (), number (), String (), or object () functions.

For example:

Number ("1")//3

String (FALSE)//"false", equivalent to False.tostring ()

"Also note: there is a ToString () method for any value except null, undefined"

Boolean ([])//true

Object (3)//new number (3)

The ToString () method defined by the number class can receive a representation of the conversion cardinality (radix) "Conversion cardinality refers to binary, octal, hexadecimal," and does not specify that the conversion rule defaults to decimal and the radix range is between 2~36. If a string is passed through the number () conversion function, it attempts to convert it to an integer or floating-point direct quantity, which can only be converted based on decimal, and no illegal trailing characters can occur. The parseint () and parsefloat () functions (which are global functions, not from methods belonging to any class) are flexible, parseint () only resolves integers, and parsefloat () resolves integers and floating-point numbers. If the string prefix is "0x" or "0X", parseint () interprets it as a hexadecimal number, parseint () and parsefloat () will skip any number of leading spaces, parse more numeric characters as much as possible, and omit the following. If the first non-null character is an illegal numeric direct quantity, Nan is returned:

For example:

parseint ("3 Blind Mice")//3

Parsefloat ("3.14 meters")//3.14

parseint ("0xFF")//255

Parsefloat (". 1")//0.1

parseint (". 1")//0

parseint () can receive the second parameter, which specifies the cardinality of the number conversion, and the legal value range is 2~36

Implicit type conversions

Some operators in JS do an implicit type conversion. If one operand of the "+" operator is a string, it converts the other operand to a string. The unary "+" operator converts its operand to a number. A Dollar! " operator to convert its operands to a Boolean value and inverse.

For example:

x+ ""//equivalent to string (x)

+x//is equivalent to number (x) and can also be written as x-0

!! X//equivalent to Boolean (x)

object is converted to the original value

The conversion of an object to a Boolean value is straightforward: all objects, including arrays and functions, are converted to true.

All objects inherit two conversion methods:

1. ToString (): Its function is to return a string that reflects the object. The default ToString () method does not return an interesting value

2, ValueOf (): If there is any original value, it will by default convert the object to the original value that represents it. Objects are composite values, and most objects cannot really be represented as an original value, so the default valueof () method simply returns the object itself instead of returning a raw value.

The conversion of an object to a string in JS follows these steps:

① This method is called if the object has the ToString () method. If a primitive value is returned, JS converts the value to a string (if it is not a string itself) and returns the string result

② if the object does not have a ToString () method, or if the method returns not a primitive value, JS calls the ValueOf () method, if this method exists. If the return value is the original value, JS converts the value to a string (if it is not a string itself) and returns the string result

③ Otherwise, JS cannot get a raw value from ToString () or valueof (), so it throws a type error exception

JS in the object-to-digital conversion process, JS did the same thing, but it will first try to use the valueof () method

① if the object has a valueof () method, or returns a primitive value, JS converts the original value to a number (if necessary) and returns the number

② Otherwise, if the object has the ToString () method, which returns a raw value, JS converts it and returns

③ Otherwise, JS throws a type error exception

JavaScript type conversion

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.