How to Convert to a String in JavaScript
What you’ll build or solve
Convert values to strings in JavaScript using String(), .toString(), or template literals. You’ll choose the right method based on safety and readability.
When this approach works best
Use this approach when you:
Learn JavaScript on Mimo
- Display numeric values in the UI.
- Build dynamic messages from variables.
- Concatenate values into readable output.
Avoid converting too early when:
- You still need the value as a number for calculations.
- You rely on strict type comparisons later.
Prerequisites
- A
.jsfile or browser console - Basic knowledge of variables and data types
No additional setup is required.
Step-by-step instructions
1) Choose the right string conversion method
JavaScript provides three primary ways to convert values to strings. Pick the one that matches your situation.
Option A: Use String() for safe conversion
String() works for almost any value and does not throw errors for null or undefined.
const num = 42;
const bool = true;
const value = null;
console.log(String(num)); // "42"
console.log(String(bool)); // "true"
console.log(String(value)); // "null"
Use String() when:
- The value might be
nullorundefined. - You want a safe, predictable conversion.
Option B: Use .toString() for known values
Most types provide a .toString() method.
const num = 100;
console.log(num.toString()); // "100"
const active = false;
console.log(active.toString()); // "false"
Be careful:
const value = null;
// value.toString(); // Error
Use .toString() only when you are certain the value is not null or undefined.
Option C: Use template literals for readable conversion
Template literals automatically convert values inside ${}.
const name = "Alex";
const age = 28;
const message = `User ${name} is ${age} years old`;
console.log(message);
This method is ideal for dynamic strings.
What to look for
Arrays convert to comma-separated strings:
String(["HTML", "CSS"]); // "HTML,CSS"
Objects convert to "[object Object]" by default:
String({ name: "Sam" });
For readable object output, use:
JSON.stringify({ name: "Sam" });
For custom array formatting, use:
["HTML", "CSS"].join(" | ");
JSON.stringify() is serialization, not simple type conversion, but it produces readable output for objects.
Examples you can copy
Example 1: Display a price label
const price = 19.99;
const label = "Price: $" + String(price);
console.log(label);
Example 2: Create a status message
const isLoggedIn = true;
const message = `Login status: ${isLoggedIn}`;
console.log(message);
Example 3: Convert mixed input safely
function showValue(value) {
return `Value entered: ${String(value)}`;
}
console.log(showValue(123));
console.log(showValue(null));
console.log(showValue(false));
Common mistakes and how to fix them
Mistake 1: Calling .toString() on null
What you might do:
const value = null;
value.toString();
Why it breaks: null has no methods.
Fix:
String(value);
Mistake 2: Accidental string concatenation
What you might do:
console.log("5" + 5);
Why it causes confusion: The number is converted automatically and concatenated.
Fix:
console.log(String(5 + 5));
Mistake 3: Seeing "[object Object]" unexpectedly
What you might do:
const user = { name: "Alex" };
console.log("User: " + user);
Why it happens: Objects convert to a default string representation.
Fix:
console.log("User: " + JSON.stringify(user));
Troubleshooting
If you see “Cannot read properties of null,” avoid .toString() on null.
If output is "[object Object]", use JSON.stringify().
If numbers concatenate unexpectedly, check for string operands.
If no output appears, confirm you are running the correct file.
If Node shows nothing, add a console.log at the top to confirm execution.
Quick recap
- Use
String()for safe conversion. - Use
.toString()for known non-null values. - Use template literals for readable dynamic strings.
- Arrays convert automatically to comma-separated strings.
- Use
JSON.stringify()for readable object output.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot