Preface What is data type conversion? The default data type obtained using a form or prompt is a string. In this case, addition and subtraction operations cannot be performed directly, and the data type of the variable needs to be converted. In layman's terms, data type conversion is the conversion of one data type into another. In the daily use of code, we occasionally encounter the need to convert data types, such as converting numeric types to strings, or converting null/undefined to Boolean types, etc. This article mainly discusses the following three types of conversions:
Other types are converted to strings:There are three methods //The first method var a=5; //Convert the numeric type to string var b=a.toString(); console.log(b); //console can print out the output information in the browser console.log(typeof b); //typeof can display the type of the current text //The second method var a=5; console.log(String(a));//Directly print out the content converted to string type//The third method var a=5; var b=''+a; console.log(b); //This method takes advantage of the fact that if there is a plus sign in JS, then starting from the first string type encountered, all subsequent ones will be converted to string types If the Boolean type is converted to a string type var a=true; console.log(String(a));//Choose any one of the three types above The result after conversion is still true But if we use console.log(typeof String(a)); After verification, you will find that although the display is still true, the type has been converted to a string type. Other types are converted to numeric typesThere are also three methods //The first method var a='1'; var b=Number(a); console.log(b); //Content is a character type conversion of a numerical value, the final display result is the original value var c=Number('c'); var d = Number (null); //Here null can be converted to 0 var e = Number(undefined); console.log(c,d,e); //The output result is NaN 0 NaN //NaN means not a number Note: If you convert a string type to a numeric type, the content of the string must be a number. If not, NaN will be displayed. //The second method //int represents integer value var a=parseInt('5'); var b = parseInt('q12'); var c = parseInt(null); var d = parseInt(undefined); console.log(a,b,c,d); //The output result is 5 NaN NaN NaN As you can see, the null in the second method is not converted to 0, but to NaN. //The third method //float represents floating point value var a=parseFloat('2.56qwe'); var b = parseFloat('2.4.6.8'); var c = parseFloat('q12'); var d = parseFloat(null); var e = parseFloat(undefined); console.log(a,b,c,d,e); //The output result is 2.56 2.4 NaN NaN NaN When the conversion type is a floating point value By default, the output will include the numbers before the first decimal point and all valid numbers after the first decimal point, and will stop when a character or the second decimal point is encountered. Other types are converted to Boolean typesThere is only one way var a=Boolean('0'); var b = Boolean(0); var c = Boolean('5'); var d = Boolean(null); var e = Boolean(undefined); var f=Boolean('');//The string content is empty var g=Boolean(' ');//The string content is a space console.log(a,b,c,d,e,f,g); //The output result is true false true false false false true Note: If a string is converted to a Boolean type, the conversion result is true as long as there is content in the string (spaces are considered content), and false if the string is empty. SummarizeThis is the end of this article about JavaScript data type conversion. For more information about JavaScript data type conversion, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Installation method of mysql-8.0.17-winx64 under windows 10
>>: Linux firewall iptables detailed introduction, configuration method and case
The 2008.5.12 Wenchuan earthquake in Sichuan took...
After Ubuntu 20.04 is installed, there is no root...
Elastic stack, commonly known as ELK stack, is a ...
This article describes how to install MySQL 5.7 f...
Talk about the scene Send Email Embedding HTML in...
Note: sg11 Our company only supports self-install...
If a form field in a form is set to disabled, the ...
Table of contents 1. Tool Introduction 2. Workflo...
In the table header, you can define the dark bord...
In the previous article, I introduced the detaile...
Unfortunately, the MYSQL_DATA_TRUNCATED error occ...
This article will introduce how to use radial-gra...
Table of contents Method 1 1. Configuration and i...
Page replacement algorithm: The essence is to mak...
Today is another very practical case. Just hearin...