JavaScript Display type conversion

Source: Internet
Author: User

First, the Transformation function Boolean ()

The values of all types in ECMAScript have values that are equivalent to the Boolean value (True and false). To convert a value to its corresponding Boolean value, you can call the Transform function Boolean ()

data type value converted to true The value converted to false
boolean true false
string any non-empty string "" (empty string)
number 0 and Nan
object Any object null
undefined not undefined
Boolean (null);//falseboolean (undefined);//falseboolean ("");//falseboolean (0);//falseboolean ({});//true

Converts a space string with a Boolean, which is also converted to true

Second, numerical conversion

There are three functions that can convert non-numeric values to numeric values: Number (), parseint (), parsefloat ()

The transformation function number () can be used for any data type, and the other two is dedicated to converting a string to a numeric value

1. Transformation function Number ()

The conversion rule for the number () function:

(1) If the Boolean value, true to convert to 1,false to 0

(2) If NULL, returns 0

(3) If undefined, return Nan

(4) as a string, the rule:

    • Converts a string to a decimal value if it contains only numbers (including the preceding sign)
    • If the string contains a valid floating-point format, convert it to the corresponding floating value
    • If the string contains a valid hexadecimal format, such as "OXF", convert it to a decimal integer value of the same size
    • If the string is empty, convert to 0
    • Converts a character to Nan if it contains characters other than the above format

(5) If it is an object, call the ValueOf () method of the object, and then convert the returned value according to the previous rule, if the result is Nan, call the ToString () method of the object, and then convert it according to the previous rule.

Console.log (Number ("Ad12")),//nanconsole.log (number ("12AD")),//nanconsole.log (number ("n")),//12console.log ( Number ("2.1w")),//nanconsole.log (Number ("")),//0console.log (number (true)),//1console.log (number ());// 0console.log (number (null)),//0console.log (number (undefined)),//nanconsole.log (Number ("")),//0console.log ( Number ("")),//0console.log (number ("0xf")),//15console.log (number ("070")),//70

2. parseint () function

Rules:

(1) Ignore the space in front of the string until the first non-whitespace character is found, and if the first character is not a number or minus sign, it will return Nan, and an empty string with parseint () will return Nan

(2) If the string starts with "ox" and is followed by a numeric character, it is treated as a hexadecimal integer

(3) The second parameter represents the cardinality (how many bytes) to use when converting

     Console.log (parseint (""));//nan     Console.log (parseint ());//nan          Console.log (parseint (true));//nan     Console.log (parseint ("0x10")),//16     Console.log (parseint ("0xf")),//15     Console.log (parseint ("070"));//70     Console.log (parseint ("22.7")),//22     Console.log (parseint ("22.7.9")),//22     Console.log (parseint ("AF") ),//175     Console.log (parseint ("2"),//2     console.log ("parseint", 8);//8     Console.log ( parseint ("10", 16));//16     

3. Parsefloat () function

Like the parseint () function, each character is parsed from the first character to the end of the string, or resolved to an invalid floating-point numeric character.

The first decimal point in the string is valid, and the second decimal point is invalid.

Only decimal numbers can be parsed, no second argument

     Console.log (parsefloat ("10qq")),//10     Console.log (parsefloat ("0xAF")),//0     Console.log (parsefloat (" 23.1.4 "));//23.1

  

Three, string conversion

1. ToString () method

Returns the string representation of the corresponding value

Numeric, Boolean, object, and string values have the ToString () method. But null and undefined do not have this method

You can pass a parameter: the cardinality of the output value. By passing parameters, you can output string values in binary, octal, hexadecimal, and other valid format representations

var age=11;console.log (age.tostring ());//"One" var B1=true;console.log (b1.tostring ());//"true" Var num=10; Console.log (Num.tostring ());//"Ten" Console.log (Num.tostring (2));//"1010" Console.log (num.tostring (8));//"12" Console.log (num.tostring);//"Ten" Console.log (num.tostring);//"A"

2, String () function

Follow the rules:

(1) If the value has the ToString () method, call this method

(2) returns "NULL" if the value is null

(3) If the value is undefined, then return "undefined"

var value1=10;var value2=true;var Value3=null;var Value4;console.log (String (value1));//"Ten" Console.log (String ( value2));//"True" Console.log (String (VALUE3));//"Null" Console.log (String (value4));//"Undefined"

  

JavaScript Display 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.