Converting to Strings
In this lesson, we'll learn how to convert various data types to strings. Let's begin!
Almost all values (including reference values and primitive values) have a toString()
method that can be used to convert the value to a string. If the value is a Number
, you can pass an integer argument to toString()
, which represents the base of the conversion. Its value must be between 2 and 36:
var num = 73;console.log(num.toString(2)); // 1001001console.log(num.toString(8)); // 111console.log(num.toString(16)); // 49var num1 = 17.25;console.log(num1.toString(2)); // 10001.01
As you can see from this short code ...