JavaScript type conversion rules

Source: Internet
Author: User
Tags logical operators numeric value

Note: JavaScript is weak and loosely relative to other strongly typed languages (such as C,c++,java, which declare variable types at the same time as the variable is defined) (it is not necessary to declare the variable type at the time of definition, but to assign a value to what type it is when it is used). Therefore, the variable types in JavaScript are also dynamically changed.

var n=88; The variable n is first defined and initialized to 88 (integer) n= "Hello world!"; At this point, the variable is assigned a string of characters, the natural variable n is (string type) n={}; At this point, the variable is assigned to the object, and the natural variable n is (object type)
Note : In real development, it is not recommended to dynamically change variable types frequently

One, explicit conversion

1.ParseInt (String,radix) Where: string is a required parameter (converted string), radix cardinality is used to limit the number of binary (between 2~36) you want to turn, if Radix is omitted or 0 (for decimal), if "ox" or "ox" is 16 binary, Exceeding 2~36 returns Nan (non-numeric not a number).

parseint ("Hello csser!"); Nanparseint ("0x8"); 8parseInt (""); Nanparseint ("020DD"); 20parseInt ("070"); 70parseInt ("22.5"); 22

2.Number (String) If the argument is a Date object, the number of milliseconds to date is returned, and Nan is returned if the argument cannot be converted to a number.

<script type= "Text/javascript" >var test1= new Boolean (true); Returns 1var Test2= new Boolean (False) if the Boolean value is true; If the Boolean value is False, the 0var test3= new Date () is returned; If it is a Date object, returns the number of milliseconds since the Var test4= new String ("999"); If the string contains only a number, the number itself is returned as Var test5= new string ("999 888"); Returns Nan if the string contains spaces, etc.
var test6= new String (""); If blank (null), returns 0document.write (number (test1) + "<br/>");d ocument.write (number (test2) + "<br/>"); document.write (number (TEST3) + "<br/>");d ocument.write (number (TEST4) + "<br/>");d ocument.write (number ( TEST5) + "<br/>");
document.write (number (TEST6) + "<br/>");</script> output Result: 101256657776588999NaN
0

3.ParseFloat () Converts a string to a floating-point number type.

Parsefloat parses its string argument into a floating-point number and returns it. If a character other than a sign (+ or-), a number (0-9), a decimal point, or an exponent (e or E) in scientific notation is encountered during parsing, it ignores the character and all subsequent characters, returning the currently resolved floating-point number. Whitespace characters at the top of the parameter string are ignored.

If the first character of the argument string cannot be parsed into a number, parsefloat returns NaN.

Note : Spaces at the beginning and end are allowed.

Tip : If the first character of a string cannot be converted to a number, parsefloat () returns NaN.

The 4.toString () method converts a Number object to a string and returns the result.

Syntax: numberobject.tostring (radix).

Radix is optional. An integer that specifies the cardinality of the number that makes 2 ~ 36. If this argument is omitted, cardinality 10 is used. Note, however, that if the parameter is a value other than 10, the ECMAScript standard allows the implementation to return any value.

<script type= "Text/javascript" >var number = new Number (1337);d Ocument.write (number.tostring ()) </script> Output: 1337

Two, implicit conversion

In some cases, even if we do not provide a display conversion, JavaScript will also perform automatic type conversion, mainly in the following cases:

1. Functions for detecting non-numeric values: IsNaN (Mix)

The IsNaN () function, tested to find that the function attempts to convert the parameter value with number () and returns true if the result is "non-numeric", otherwise false.

2. Increment decrement operator (including front and rear), unary plus sign operator

These operators apply to values of any data type, and for different types of values, the operator follows the following rules (which, by contrast, have the same rules as the number () rule):

1. If it is a string that contains a valid numeric character, it is converted to a numeric value (the conversion rule is the same as number ()), and the string variable becomes a numeric variable when the operation is performed plus minus 1.

2. If it is a string that does not contain a valid numeric character, set the value of the variable to nan and the string variable into a numeric variable.

3. If it is a Boolean value of false, first convert it to 0 and then perform the operation plus minus 1, boolean variable programming numeric variable.

4. If the Boolean value is true, convert it to 1 and then perform the plus minus 1 operation, and the Boolean variable becomes a numeric variable.

5. If it is a floating-point value, perform the plus minus 1 operation.

6. If it is an object, first call the object's valueof () method, and then apply the preceding rule to the return value. If the result is Nan, then the ToString () method is called before the preceding rule is applied. The object variable becomes a numeric variable.

Small quiz:

Perform a post-increment operation on the following types of values, what is the result?

"2″," 02DD "," "", false, 22.5, + "",-false, +new Date ()

3. Addition operator

The plus operator is also used in JavaScript for string connectors, so the rules for the plus operator are divided into two cases:

? If the two operation values are numeric, the rule is:

1. If one of the operands is Nan, the result is Nan

2. If it is infinity+infinity, the result is Infinity

3. If it is-infinity+ (-infinity), the result is-infinity

4. If it is infinity+ (-infinity), the result is Nan

5. If it is +0+ (+0), the result is +0

6. If it is (-0) + (-0), the result is-0

7. If it is (+0) + (-0), the result is +0

? If there is an action value of string, then:

1. If the two operation values are strings, stitch them together

2. If only one action value is a string, convert the additional action value to a string and then stitch it up

3. If an operand is an object, numeric value, or Boolean, call the ToString () method to get the string value, and then apply the preceding string rule. For

Undefined and null, respectively, calling string () to be explicitly converted to strings.

As you can see, in an addition operation, if there is an action value of type string, the other action value is converted to a string and finally concatenated.

4. Multiplication, minus operator, modulo operator

These operators are for operations, so they have commonality: if one of the operands is not a numeric value, the number () function is implicitly called to convert. For detailed rules for each operation, refer to the definition in ECMAScript.

5. Logical operators (!, &&, | | )

Logical NON (! Operator first converts its operation value to a Boolean value through the Boolean () function, and then the negation.

Logical AND (&&) operators, if an action value is not a Boolean value, follow these rules to convert:

1. Returns the second action value if the first operand is converted to true after a Boolean (), otherwise returns the first value (not the Boolean () converted value)

2. If there is an action value of NULL, returns null

3. If there is an action value of Nan, return Nan

4. If there is an action value of undefined, return undefined

Logic or (| | ) operator, if an action value is not a Boolean value, follow these rules:

1. Returns the second action value if the first action value is converted to False after a Boolean (), otherwise returns the first action value (not a Boolean () converted value)

2. Processing rules for undefined, nulls, and Nan are the same as for logic (&&)

6. Relational operator (<, <=;, >=)

As with the above operators, the operation values of the relational operators can also be of any type, so it is also necessary for the system to perform implicit type conversions when using non-numeric types to participate in comparisons:

1. If the two operation values are numeric, a numeric comparison

2. If the two action values are strings, compare the character encoding values of the strings

3. If only one operation value is numeric, convert another operation value to a numeric value to compare

4. If an operand is an object, call the ValueOf () method (if the object does not have a valueof () method to call the ToString () method), the resulting results follow the previous

Rule execution Comparison

5. If an action value is a Boolean, convert it to a numeric value, and then compare

Note: Nan is a very special value that is not equal to any type of value, including itself, and returns False when it compares size to any type of value.

7. Equality operator (= =)

The equality operator compares an implicit conversion of an action value:

1. If an action value is a Boolean, convert it to a numeric value before comparing it

2. If one action value is a string and the other action value is a numeric value, the string is converted to a numeric value by the number () function

3. If an action value is an object and the other is not, call the object's valueof () method, and the resulting result is compared with the previous rule

4.null is equal to undefined

5. If an action value is Nan, the equality comparison returns false

6. If the two action values are objects, compare whether they point to the same object

JavaScript type conversion rules

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.