點燈坊

失くすものさえない今が強くなるチャンスよ

4 Ways to Convert from Number to String

Sam Xiao's Avatar 2021-12-19

We can convert from Number to String by String, toString, Type Coercion or Template String.

Version

ECMAScript 2015

String

let x = 123
String (x) // ?
  • String is a constructor function to create a Wrapper Object by new
  • String is a normal function to convert from Number to String

string000

toString

let x = 123
x.toString () // ?

We can also to use Number.prototype.toString to convert from Number to String.

string001

Type Coercion

let x = 123
x + '' // ?

When using Number + String, Number is converted to String first. So we can use + Empty String to Convert from Number to String.

string002

Template String

let x = 123;
`${x}` // ?

We can also use Template String to convert from Number to String.

string003

Conclusion

  • String and toString is the most intuitive way to convert from Number to String
  • Template String is another concise way to convert from Number to String