Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String()

Converts any value to a string.

Available since: ES1

Syntax

String(value)

Parameters

ParameterTypeDescription
valueanyThe 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,3

An 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

In the tutorial

  • Type conversion — How to turn a string into a number and back in JavaScript — Number, String, Boolean, parseInt and implicit conversion.

Thanks for reading!

Donate to the site — we work hard for you.