String()
Converts any value to a string.
Available since: ES1
Syntax
String(value)Parameters
| Parameter | Type | Description |
|---|---|---|
| value | any | The value to convert to a string. |
Return value
stringThe string form of the value.
Examples
Different values into strings:
console.log(String(42) + '!');
console.log(String(true), String(null), String(undefined));
console.log(String([1, 2, 3]));Output:
42!
true null undefined
1,2,3An object turns into useless text:
console.log(String({ name: 'Anna' }));
console.log(JSON.stringify({ name: 'Anna' }));Output:
[object Object]
{"name":"Anna"}Notes
Unlike value.toString(), the String() function also works with null and undefined. For objects use JSON.stringify().
See also
- Number() — Converts a value to a number.
- Boolean() — Converts a value to true or false.
- Number.prototype.toString — Converts a number to a string, including in another numeral system.
- Template literal — A string in backticks with substitutions and line breaks.
In the tutorial
- Type conversion — How to turn a string into a number and back in JavaScript — Number, String, Boolean, parseInt and implicit conversion.