Converting into a String

Casting a value to a String can be achieved by invoking the String constructor as a regular function (that is, not as a constructor):

String(456); // => "456"String(true); // => "true"String(null); // => "null"String(NaN); // => NaNString([1, 2, 3]); // => "1,2,3"String({ foo: 1 }); // => "[object Object]"String(function(){ return 'wow' }); // => "function(){ return 'wow' }"

Calling String() with your value is the most explicit and clear way of casting to a String, although there are more succinct patterns that are sometimes used:

'' + 1234; // => "1234"`${1234}`; // => "1234"

These two expressions may appear equivalent, and for many values, they are. But, internally, they work differently. As we'll see later, the ...

Get Clean Code in JavaScript now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.