JavaScript data type conversion example (converting other types to strings, numeric types, and Boolean types)

JavaScript data type conversion example (converting other types to strings, numeric types, and Boolean types)

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
  • Other types are converted to numeric types
  • Other types are converted to Boolean types

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 types

There 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 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 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.

Summarize

This 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:
  • Do you know JavaScript basic data type conversion?
  • Detailed explanation of JavaScript data type conversion (recommended)
  • Detailed explanation of JavaScript explicit data type conversion
  • JavaScript data type conversion principles (dry goods)
  • JavaScript basic data types and conversions
  • Summary of data type conversion in JavaScript
  • Detailed explanation of Javascript data type conversion rules
  • Comprehensive understanding of JavaScript data type conversion
  • JavaScript implements data type conversion
  • Summary of data type conversion methods in JavaScript
  • A brief discussion on JavaScript data types and conversions
  • Javascript Basic Tutorial: Data Type Conversion
  • js data type conversion summary notes
  • JavaScript data type conversion

<<:  Installation method of mysql-8.0.17-winx64 under windows 10

>>:  Linux firewall iptables detailed introduction, configuration method and case

Recommend

Detailed explanation of the 4 codes that turn the website black, white and gray

The 2008.5.12 Wenchuan earthquake in Sichuan took...

How to enable the root account in Ubuntu 20.04

After Ubuntu 20.04 is installed, there is no root...

How to build a multi-node Elastic stack cluster on RHEL8 /CentOS8

Elastic stack, commonly known as ELK stack, is a ...

How to install MySQL 5.7 from source code in CentOS 7 environment

This article describes how to install MySQL 5.7 f...

Solution for converting to inline styles in CSS (css-inline)

Talk about the scene Send Email Embedding HTML in...

How to submit the value of a disabled form field in a form Example code

If a form field in a form is set to disabled, the ...

Use IISMonitor to monitor web pages and automatically restart IIS

Table of contents 1. Tool Introduction 2. Workflo...

Build a WebRTC video chat in 5 minutes

In the previous article, I introduced the detaile...

CSS uses radial-gradient to implement coupon styles

This article will introduce how to use radial-gra...

Detailed explanation of Vue px to rem configuration

Table of contents Method 1 1. Configuration and i...

Understand the principle of page replacement algorithm through code examples

Page replacement algorithm: The essence is to mak...

About the implementation of JavaScript carousel

Today is another very practical case. Just hearin...