JavaScript Data Type Conversion Instance of Other Types to String Numeric Boolean

  • 2021-11-10 08:29:42
  • OfStack

Preface

What is data type conversion?

Using the form, the default type of data obtained by prompt is string type, so you can't directly perform addition and subtraction, but need to convert the data type of variables.

Generally speaking, data type conversion is to convert one data type to another.

In the use of common code, we occasionally encounter the need for data type conversion, such as converting numeric types to strings, or converting null/undefined to Boolean types, etc. In this article, we mainly talk about the following three types of conversion:

Convert other types to strings Other types are converted to numeric types Convert other types to Boolean types

Convert other types to strings:

There are three ways


// No. 1 1 Methods 
var a=5;// Convert numeric type to string 
var b=a.toString();
console.log(b);//console You can print out the output information in the browser 
console.log(typeof b);//typeof Can display the type of current text 
 
// No. 1 2 Methods 
var a=5;
console.log(String(a));// Print out the content converted to string type directly 
 
// No. 1 3 Methods 
var a=5;
var b=''+a;
console.log(b);
// This method takes advantage of the JS If there is a plus sign in the 1 Start with a string type, and all the rest become string types 
 

If Boolean type is converted to string type


var a=true;
console.log(String(a));// In the above 3 Choose from any of the types 1 It can be used as a seed 

The result of this conversion is still true

But if we use


console.log(typeof String(a));

After validation, you will find that although true is still displayed, the type has been converted to string type.

Other types are converted to numeric types

There are also three ways to do this


// No. 1 1 Methods 
var a='1';
var b=Number(a);
console.log(b);// Character type with numeric value is converted to numeric value, and the final display result is the original numeric 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 is NaN  0  NaN
//NaN Denote not a number

Note: If string type is converted to numeric type, the contents of the string must be numeric. If not, NaN will be displayed.


// No. 1 2 Methods 
//int Represents an 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 is 5  NaN  NaN  NaN

You can see that the null of the second method is not converted to 0, but NaN.


// No. 1 3 Methods 
//float Represents floating-point values 
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 is 2.56  2.4  NaN  NaN  NaN

When the conversion type is a floating-point numeric value

The number before the first decimal point and all significant numbers after the first decimal point will be output by default, such as characters encountered or the second decimal point stopped.

Convert other types to Boolean types

There 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 contents of the string are spaces 
 
console.log(a,b,c,d,e,f,g);
 
// The output is true  false  true  false  false  false  true

Note: If the string is converted to a Boolean type, the conversion result is true as long as there is content in the string (if the content is space, then there is content), and if the string content is empty, it is false.

Summarize


Related articles: