+ 4
There are a lot of ways. The most simple one would be to just add an empty string to the variable, e.g.
var str = number + "";
or
var str = "" + number;
With ES6, using a template literal is also possible:
var str = '${number}';
The most obvious and direct way would be an explicit conversion, either through the toString() method, or the constructor:
var str = number.toString();
var str = String( number );
I'd favor the toString() method though, as it communicates its intend in the best way, making it easier to read for others.
- 2
Can anyone help me with this code:”hello world!”?