string conversion of JavaScript

Source: Internet
Author: User

Other data types, such as numbers, Booleans, and so on, can be converted to strings; In general, the scripting engine automatically completes such conversions based on context. For example, when a numeric or Boolean variable is passed to a function that expects to receive a string variable, the value is implicitly converted to a string and then processed:

var num_value=35.00; alert (num_value);   // expected as a string

In addition, if you want to perform an addition operation on two variables in an assignment statement, where one is a string variable and the other is a numeric variable, the number variable is automatically converted to a string, followed by the two strings:

var num_value=35.00; var string_value= "This is a number:" +num_value;

When to convert a number to a string depends on when the JavaScript scripting engine handles the string. For example, if a string is the first of a sequence value, all values are treated as strings:

var strvalue= "4" + 3 + 1;  // The result is "431." var strvalue=4 + 3 + "1";  // The result is

However, if you use a different action symbol (in addition to the plus sign), the string is converted to a number:

var firstresult= "3";  // subtraction operation, the result is var secondresult= "3";  // multiplication operation, the result is 9 var thirdresult=30/"3";  // division operation, the result is ten

Implicit conversions depend on the position of the operator and the variable, which more fully reflects the danger of a loose type: The value varies with context, depending on the order in which new data type operations are introduced, and the operators that are referenced.

string conversion of 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.