A comprehensive understanding of data type conversion in JavaScript

  • 2021-07-01 06:35:49
  • OfStack

First of all, because JavaScript is a weakly typed language (something of a weakly typed language has no obvious type, He can follow the different circumstances, Automatically changing types and strong types have no such provisions. Operations between different types are strictly defined, Only variables of the same type can be operated, although the system also has a definite default conversion, which is never as casual as weak types, that is to say, variables do not need to specify data types when declared, and variables are determined by assignment operations), so there are implicit conversions in JavaScript type conversions that strongly typed languages do not have.

1.1 Implicit Conversion (Automatic Type Conversion) in JavaScript

Simple definition: Different types of data in the operation of the default data type conversion.
Implicit conversion usually follows the following rules:

1. Numbers + Strings: Numbers are converted to strings.


var n1=12;//number Type 
  var n2="12";//string Type 
  console.log(n1+n2);// The result is string Type of "1212"

2. Number + Boolean: true is converted to 1, and false is converted to 0.


var n1=12;//number Type 
  var n2=true;// Boolean type 
  console.log(n1+n2)// The result is 13

3. String + Boolean value: Boolean value is converted to true or false.


var n1="Hello";//string Type 
  var n2=true;
  console.log(n1+n2);// The result is string Type of "Hellotrue"

4. Boolean + Boolean


var n1=true;
  var n2=true;
  console.log(n1+n2);// The running result is 2;

For the results of the above case, small partners who are not sure of the output type can view the current type of the variable through the typeof () method.


 console.log(typeof(11));//number
  console.log(typeof("11"));//string
  console.log(typeof(true));//boolean

1.2 Data Type Conversion Function

There are implicit conversions in JavaScript, and there will be explicit conversions corresponding to them. To perform explicit conversions, the following functions are needed:

1. toString()

---- > To string, all data types in JavaScript can be converted to string type


var n1="12";
  var n2=true;
  var n11=toString(n1);
  var n22=toString(n2);
  console.log(typeof(n11));// The result is string
  console.log(typeof(n22));// The result is string

2.parseInt()

---- > Parses out an integer part of type string or number, and returns NaN (not a number) if there is no part that can be converted


var n1="12";
  var n2="12han";
  var n3="Hello";
  var n11=parseInt(n1);
  var n22=parseInt(n2);
  var n33=parseInt(n3);
  console.log(n11);// The result is 12
  console.log(n22);// The result is 12
  console.log(n33);// The result is NaN

Running the above code, it is not difficult to see that the data types converted by variables n1 n2 n3 are all number, but the function n33 obtained by function parseInt () is not the number type value we know, but NAN, which is not difficult to see that although NaN is not a number, it belongs to the number type, but it cannot be applied to any algorithm of ordinary numbers, and it is a special existence. (It will be mentioned in the following blog post, so I won't repeat it again.)

3.parseFloat()

---- > Parse out the floating-point part of an string, and return NaN (not a number) if there is no part that can be converted.


var n1="12.4.5";
  var n2="12.4han";
  var n3="Hello";
  var n11=parseFloat(n1);
  var n22=parseFloat(n2);
  var n33=parseFloat(n3);
  console.log(n11);// The result is 12.4
  console.log(n22);// The result is 12.4
  console.log(n33);// The result is NaN

From the above example, we can conclude that the return value of parseFloat () function is indeed a number, but from the vertical comparison of several variables, we can easily see that the function will not be converted after encountering the second decimal point, so special attention should be paid here.


Related articles: