Served with JavaScript

How to convert one data type to another data type in JavaScript

Even if JavaScript is a loosely typed language, you may need to convert a value from one type to another.

In JavaScript, we have the following primitive types:

  • Number
  • String
  • Boolean
  • Symbol

And object type:

  • Object

(plusnullwithundefined, But it makes no sense to project from/to them)

For example, you might want to convert:

  • A number to a string
  • String to number
  • Boolean string
  • Boolean value of string

…and many more.

This is a technique that can be used to convert from one type to another. I introduced the most common situations.

Convert to string

Usually, to convert from anything to a string usually just calltoString()The method processes any value, and JavaScript creates a string value corresponding to the type. Or you can pass any value toString()Global function.

Conversion from number to string

Use string global functions or numeric typestoString()method:

String(10) //"10"
(10).toString() //"10"

Convert from boolean to string

Use String global function or Boolean typetoString()method:

String(true) //"true"
true.toString() //"true"
String(false) //"false"
false.toString() //"false"

Conversion from date to string

Use string global functions or date typestoString()method:

String(new Date('2019-01-22'))
//"Tue Jan 22 2019 01:00:00 GMT+0100 (Central European Standard Time)"

(new Date('2019-01-22')).toString()
//"Tue Jan 22 2019 01:00:00 GMT+0100 (Central European Standard Time)"

Special cases with strings

String(null) //"null"
String(undefined) //"undefined"
String(NaN) //"NaN"

Convert to number

Convert from string to number

We can useNumber()Global functions are a bit like constructors. We can pass it a string, and JavaScript will figure out how to convert it to a number:

Number("1") //1
Number("0") //0

Trim the string before converting it to a number:

Number(" 1 ") //1

Passing an empty string defaults to 0:

Number("") //0

To use decimals, you can use dots:

Number("12.2")

If the string contains invalid characters, it will generate aNaN.

This is the basis for conversion to numbers, but I provide more details in itHow to convert a string to a number in JavaScript. There are other ways to generate numbers from strings, includingparseInt(),parseFloat(),Math.floor()One yuan+operator.

Convert from boolean to number

Just like we did with strings, pass the boolean value toNumber()Will return 0 or 1:

Number(true) //1
Number(false) //0

Conversion from date to number

If you pass the Date object toNumber(), It will return a date timestamp, which is the best digital conversion date you can get.

Special cases with numbers

Number(null) //0
Number(undefined) //NaN
Number(NaN) //NaN

Convert to boolean

Any value can be converted to boolean and passed toBoolean().

All values will be parsed astrueapart from:

Boolean(false) //false
Boolean(0) //false
Boolean(NaN) //false
Boolean("") //false
Boolean(null) //false
Boolean(undefined) //false

Download mine for freeJavaScript beginner's manual


More js tutorials: