Search⌘ K
AI Features

Converting to Strings

Explore how to convert different JavaScript values to strings using the toString method with optional radix and the String() casting function. Understand conversion rules for numbers, null, and undefined to handle string transformations effectively.

We'll cover the following...

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:

Javascript (babel-node)
var num = 73;
console.log(num.toString(2)); // 1001001
console.log(num.toString(8)); // 111
console.log(num.toString(16)); // 49
var num1 = 17.25;
console.log(num1.toString(2)); // 10001.01

As you can see from this short ...