Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JS Puzzles · Const

String coercion

String coercion converts a value to text when JavaScript needs its string form. It happens implicitly in suitable operations or explicitly with String(value).

How it works

JavaScript converts a value to text when an operation needs a string, such as displaying it. This conversion can happen implicitly, or explicitly with String(value). Common values receive predictable textual forms: booleans become "true" or "false", null becomes "null", and numbers become their textual representation.

Syntax

String(value)

Parameters

NameTypeDescription
valueanyThe value whose textual representation is required.

Returns

A string representation of the supplied value.

Key points

  • Operators and functions may perform implicit conversion when their required input type differs from the supplied value.
  • String coercion supplies a value's textual representation when an operation needs text.
  • Use String(value) for an explicit conversion instead of relying on an operation to infer the desired type.
  • Boolean false and null convert to the literal strings "false" and "null".
  • String conversion is one of JavaScript's three commonly used type conversions, alongside number and boolean conversion.

Common mistakes

  • String conversion does not produce an empty string for null or booleans: null becomes "null", while false becomes "false".

Example: Explicit and implicit string conversion

let value = true;
console.log(String(value)); // "true"
console.log(String(null));  // "null"
console.log("" + 1 + 2);   // "12"

Use String(value) when an explicit string conversion is intended; concatenation can produce different results because + handles strings and numbers differently.

When to use it

Output formatting

Displaying a value through an API or output operation that requires text.

Explicit normalization

Making the conversion intent explicit before storing or passing textual data.

User-facing messages

Converting booleans or null into predictable text for labels or messages.

Interview questions

QWhen does string coercion occur, and how can it be requested explicitly?

String coercion occurs when JavaScript needs the string form of a value, such as while displaying it. It can also be requested explicitly with String(value).

Follow-up: What does String(false) return? It returns the string "false", not the boolean false.

QWhat are the results of converting a boolean and null to strings?

String(true) produces "true", and String(null) produces "null". The result is a string even though the inputs have different types.

Follow-up: How would you verify the result type? Apply typeof to the converted value; it returns "string".

QWhy should explicit String(value) be preferred when text is required?

String conversion is distinct from numeric conversion: String(value) creates textual output, whereas arithmetic operators may convert operands to numbers. Use String when the required result is a string.

Follow-up: What happens when a string is passed to Number instead? A numeric string can become a number, while an invalid numeric string becomes NaN.

  • Number conversion
  • Boolean conversion
  • Type conversions

Source: Type Conversions. Checked 2026-10-15.