Type conversions in JavaScript

Source: Internet
Author: User
Tags string to number

1. Implicit type conversion

①== if the two variables of the equality operation are not of the same type, then the equality operator attempts some type conversions:

    • If one is null , One is undefined , They ;
    • Span style= "font-family:"microsoft yahei"" If one value is a number and the other is a string, First convert the string to a number , and then use the converted value for comparison;
    • If one is true , convert it to 1 again, and if one is false, convert it to 0 and then compare;
    • If one value is an object and the other value is a number or string, Convert the object to the original value , and then compare it. The object is converted to the original value by the ToString () or valueof () method. JS language core built-in class first try to use valueof (), and then try to use ToString (), in addition to the date class, the date class only use the ToString () Conversion.

②< > <= >= comparison Operator. Operands can be of any type, but only numbers and strings can actually perform a comparison operation .

    • If the operand is an object, the object is first converted to the original value: if valueof () returns a raw value, the original value is used Directly. otherwise, the ToString () conversion is used;
    • After the object is converted to the original value, if the two operands are strings, then the string is compared;
    • After the object is converted to the original value, if at least one operand is not a string, then two is converted to a number for comparison . If one is nan, the result is False.

The ③-minus operator converts two operands to a number .

④+ operator, If one operand is a string, the other operand is also converted to a string . a unary plus converts its operand to a number .

⑤! Convert to a Boolean value and take the Inverse. !! X is equivalent to Boolean (x).

2. Explicit type conversions

① uses the Boolean ()/number ()/string ()/object () Function. Except for null and any value of undefined has the ToString () method, The result of this method is usually the same as the return result of the string () method.

② Numeric to String

    • The toString () method, which accepts parameters and compares them into a few binary.
    • ToFixed (), The parameter indicates that several decimals are Reserved.
    • Toexponential (), The parameter indicates that several decimal points are reserved.
    • Toprecision (), The parameter represents the number of significant digits.

③ String to Number

    • parseint () function
    • The parsefloat () function, which is a global function, is not a method of any class .

④ object to original value

    • ToString () Method.
    1. The array class converts each element to a string and adds a comma- synthesized string between the elements;
    2. The function class returns the representation of the implementation definition of this Function.
    3. The date class returns a readable date and time string.
    4. The RegExp class is converted to direct literals.
    • ValueOf () Method. If an object has any original value, he will by default convert the object to the original value that represents it. however, The object is a composite value, and in most cases valueof () returns the object itself.
    • The object-to-string conversion uses the ToString () method first, and the Object-to-number conversion uses the valueof () method First.

Type conversions in JavaScript

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.