JavaScript type conversion (Conversion from character to digit to character) _ javascript skills

Source: Internet
Author: User
Type conversion in JavaScript cannot be regarded as forced type conversion. In Java, the forced conversion between basic types is not like this. For example, to convert Integers to strings, you must use Integer. toString () static method or String. the valueOf () static method converts a string to an Integer and must use an Integer. valueOf ().
It can be seen that the type conversion in JavaScript cannot be regarded as "forced type conversion ".
In JavaScript, both the Double and Int types are regarded as Number objects. Therefore, both typeof 1 and typeof 1.0 return number. In this way, we don't need to worry about whether it is Int or Double type, so that the JavaScript interpretation engine can handle it internally.

If you want to convert Number to String, you can use the toString () method of Number, such as (1 ). toString () brackets must be 1. the toString () space is required. Otherwise, compilation errors may occur. If it is a variable, no space is required.) or the String () function is called. Both methods automatically call the NumberToString () in the interpretation engine (), or call other functions in the hexadecimal format.
If you want to convert a String to a Number, you can use the Number () function, which automatically determines whether the String is an integer or a floating-point Number, and then uses the corresponding data type internally, you can also use the global functions parseInt () and parseFloat () to convert them according to your requirements. Similarly, they use StringToNumber, StringToInt, and other internal functions in the internal mechanism of the interpretation engine.
To convert a Double value to an Int value, you must use the Math. floor () function (rounded to the end) or Math. round () (rounded to the nearest integer)
Int is converted to Double, so you do not need to consider any issues. You can directly use Int as Double for calculation.
Note: Number and String functions are special functions. In the JS engine, they will automatically determine whether they are called as constructors or normal calls. Therefore, they can use the new keyword, it can also be called directly as a function.
For the JS reference manual, Microsoft has a very good CHM, which is available in both the guide and API reference. It is a Windows Script technology and a Chinese version. I downloaded it on MSDN. For details about the JS interpretation engine, I refer to the Spidermonkey of Netscape, which is now maintained by Mozilla.

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.