Start 2 of JavaScript programming

Source: Internet
Author: User

When a variable is used in an expression, JavaScript automatically converts a type value to another type. That is to say, there can be different types of data combinations in the expression. Javascript completes the type conversion necessary to make the expression meaningful During computation. In JavaScript, type conversion is based on the priority of its operation. Generally, string operations have a higher priority, followed by floating point operations, integer operations, and logical operations.
For example, if the expression "test" + 5 is used, convert the number 5 to the string "5" and append it to the string "test". The result is test5.
Parseint is converted to an integer.

Parsefloat is converted to a floating point number.

Parsefloat () function: similar to the parseint () function, it returns the first floating point number contained in the string. If the string does not start with a valid floating point number, 0 is returned.

For example, parsefloat ("2.1e4xyz") returns 21000, while parsefloat ("XYZ") returns 0.

Three methods for converting basic data types:

1. Convert to string type: string (); for example, the result of string (678) is "678"
2. Convert to numeric type: Number (); for example, the result of number ("678") is 678.
3. Convert to boolean: Boolean (); for example, Boolean ("AAA") returns true

How to extract another type of value from one value:

1. Extract the integer parseint () in the string. For example, the result of parseint ("123 Zhang") is 123.
2. Extract the floating point: parsefloat () in the string. For example, the result of parsefloat ("0.55 Zhang") is 0.55.
3. Execute a piece of JavaScript code represented by a string: eval (); example: Zhang = eval ("1 + 1") Result Zhang = 2

Note: eval () function: converts a string expression to a numeric value. For example, the result of the statement Total = eval ("432.1*10") is total = 4321, which is about to assign the value 4321 to the total variable.

In the following example, when an addition operation is executed, the numeric value is converted to a string value. When a multiplication operation is executed, the string value is converted to Nan (not a number ).

<HTML>
<Head>
<Title> display of special characters </title>
</Head>
<Body>
<Script language = "JavaScript" type = "text/JavaScript">...
<! --
VaR mystring = "test ";
VaR num = 1;
Document. Write (mystring + num); document. Write ("<br/> ");
Document. Write (Num + mystring); document. Write ("<br/> ");
Document. Write (mystring * num); document. Write ("<br/> ");
Document. Write (Num * mystring );
// -->
</SCRIPT>
</Body>
</Html>

Shows the result displayed in IE.

In the preceding example, the "+" operator can perform both numerical operations and string operations (equivalent to String concatenation), so string operations are performed in priority; the "*" operator can only operate on numeric values. So what is the significance of multiplying a string by a number? Meaningless. In this case, the string value is converted to "non-number" Nan, and the number is multiplied by Nan. Of course, the result is still "non-number" Nan.
What is Nan? Nan is "not a number", that is, "not a number ". When the operation fails to return the correct value, the "Nan" value is returned. The Nan value is very special, because it is "not a number", so any number is not equal to it, or even Nan itself is not equal to Nan.

Parseint () function: converts a string to an integer. The return value starts with the first character of the string. If the character does not start with an integer, 0 is returned. The parseint () function can also convert hexadecimal or decimal numbers.

For example, parseint ("123xyz") returns 123, while parseint ("XYZ") returns 0.

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.