Back to JavaScript Notes
Topic #356

toString

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 radix argument.
  • 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 number 123 to 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() on null or undefined throws a TypeError. Use String(value) or value?.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 custom toString() method or use JSON.stringify() for data.
  • Using array toString() when you need structure. It flattens nested arrays into comma-separated text. Use JSON.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.

ApproachHandles null/undefinedBest for
value.toString()NoExplicit conversion and number radix
String(value)YesSafe conversion of unknown values
`${value}`YesReadable 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.

Want to go beyond the notes?

Join Coding Now Tech Institute's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

toString – FAQs

Quick answers about learning toString in JavaScript.

This free note from Coding Now Tech Institute explains toString in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on Coding Now Tech Institute, including toString, is 100% free with no signup required.
With focused practice, most students grasp toString in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now