By the end of this lesson, you will be able to convert JavaScript values to strings using toString(), know when it is safe, and choose it over String() or template literals.
What it is
toString() is a method that returns a string representation of a value. Most built-in JavaScript types have their own version: numbers, booleans, strings, arrays, dates, and functions. For plain objects, the inherited Object.prototype.toString() usually returns a generic tag such as [object Object].
The mental model is: “ask this value to describe itself as text.” It is explicit conversion, not automatic coercion. Related terms include primitive wrapper objects, Object.prototype.toString(), radix, and type detection.
Why it matters
- It lets you turn numbers into readable text for labels, logs, or user interfaces.
- It supports number bases, such as binary or hexadecimal, through the optional
radixargument. - It gives arrays a quick comma-separated string for simple display.
- It helps inspect built-in types when used through
Object.prototype.toString.call(value).
Syntax or steps
The smallest useful pattern is:
value.toString();
For numbers, you can add a radix from 2 to 36:
number.toString(radix);
Because number literals can be ambiguous with decimal points, wrap them in parentheses:
(123).toString(); // "123"
Example
const age = 123;
const label = age.toString();
const binary = age.toString(2);
const items = [1, 2, 3];
const list = items.toString();
const user = { name: "Ada" };
const tag = Object.prototype.toString.call(user);
console.log(label);
console.log(binary);
console.log(list);
console.log(tag);
Part by part:
age.toString()converts the number123to the string"123".age.toString(2)converts the same number to base 2, producing"1111011".items.toString()calls the array version, which joins elements with commas, producing"1,2,3".Object.prototype.toString.call(user)asks the generic object method to identify the value, producing"[object Object]".
Common mistakes
- Calling
toString()onnullorundefinedthrows aTypeError. UseString(value)orvalue?.toString() ?? ""when the value may be missing. - Writing
123.toString()causes a syntax error. Use(123).toString()or store the number in a variable first. - Expecting plain objects to produce useful text. The default object string is usually
[object Object]. Define a customtoString()method or useJSON.stringify()for data. - Using array
toString()when you need structure. It flattens nested arrays into comma-separated text. UseJSON.stringify()if you need a recoverable representation.
When to use it
Use toString() when you know the value is not null or undefined and you want the type’s own string form. Use String() when you need a safe general conversion. Use template literals when you are building text with interpolation.
| Approach | Handles null/undefined | Best for |
|---|---|---|
value.toString() | No | Explicit conversion and number radix |
String(value) | Yes | Safe conversion of unknown values |
`${value}` | Yes | Readable string building |
Practice
Guided exercise: create a variable named score with the value 255. Convert it to a decimal string and a hexadecimal string.
const score = 255;
const decimal = score.toString();
const hex = score.toString(16);
console.log(decimal);
console.log(hex);
Expected output: "255" and "ff".
Challenge: create an object with a custom toString() method that returns "User: Ada". Hint: define toString() { return "User: " + this.name; } inside the object.
Quick check
What does [1, 2].toString() return?
It returns "1,2".
Summary
toString() is a direct way to ask a JavaScript value for its string form, with useful behavior for numbers, arrays, and other built-in types. It is not universally safe for missing values, so choose String() or template literals when you need robust conversion.