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.
constnum=42;
constbool=true;
constvalue=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.
constnum=100;
console.log(num.toString());// "100"
constactive=false;
console.log(active.toString());// "false"
Be careful:
constvalue=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 ${}.
constname="Alex";
constage=28;
constmessage=`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:
CSS
String(["HTML","CSS"]);// "HTML,CSS"
Objects convert to "[object Object]" by default:
CSS
String({ name:"Sam" });
For readable object output, use:
CSS
JSON.stringify({ name:"Sam" });
For custom array formatting, use:
Bash
["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
constprice=19.99;
constlabel="Price: $"+String(price);
console.log(label);
Example 2: Create a status message
constisLoggedIn=true;
constmessage=`Login status:${isLoggedIn}`;
console.log(message);
Example 3: Convert mixed input safely
JavaScript
functionshowValue(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:
constvalue=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:
JavaScript
console.log(String(5+5));
Mistake 3: Seeing "[object Object]" unexpectedly
What you might do:
constuser= { name:"Alex" };
console.log("User: "+user);
Why it happens: Objects convert to a default string representation.
Fix:
JavaScript
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