
JavaScript provides a simpler way to convert boolean values to strings using built-in methods. The most common approach is to call the toString() method directly on the boolean value. This method returns the string "true" or "false" depending on the boolean’s value.
let flag = true; let str = flag.toString(); console.log(str); // Output: "true"
This is often preferred over concatenating a boolean with an empty string, as it makes the intention clearer and avoids any unintended side effects from type coercion.
let flag = false; let str = String(flag); console.log(str); // Output: "false"
Another idiomatic way is to use the String() function, which is a built-in global function that converts any value to its string representation. It works predictably with booleans, returning "true" or "false" just like toString().
Be aware that calling toString() on null or undefined will throw an error, but since booleans are primitive values, that is safe:
let boolValue = true; console.log(boolValue.toString()); // "true" let nullValue = null; // nullValue.toString(); // TypeError: Cannot read property 'toString' of null
If you’re working with variables that might not be strictly boolean, using String() is safer because it can handle null or undefined without throwing:
let unknownValue = null; let safeStr = String(unknownValue); console.log(safeStr); // "null"
For boolean wrapper objects created with new Boolean(), toString() behaves similarly:
let boolObj = new Boolean(false); console.log(boolObj.toString()); // "false"
However, be cautious when using Boolean objects instead of primitives, as they can introduce unexpected behavior in conditions due to truthiness rules.
Garmin vívoactive 5, Health and Fitness GPS Smartwatch, AMOLED Display, Up to 11 Days of Battery, Ivory
$174.95 (as of June 2, 2026 22:39 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Understanding implicit type coercion with booleans
JavaScript’s implicit type coercion often surprises developers, especially when booleans are involved. When a boolean is used in a context expecting a string, JavaScript automatically converts it without explicit calls to toString() or String(). This coercion happens in expressions like string concatenation or template literals.
Consider the following example where a boolean is concatenated with a string:
let flag = true; let message = "The flag is " + flag; console.log(message); // Output: "The flag is true"
Here, the boolean true is implicitly converted to the string "true". This occurs because the + operator, when used with a string, coerces the other operand to a string as well.
Template literals also rely on this implicit coercion. When embedding expressions inside backticks, JavaScript converts the value to a string automatically:
let flag = false;
let message = Flag status: ${flag};
console.log(message); // Output: "Flag status: false"
Implicit coercion also happens in contexts where booleans are used as keys or property names, which are strings internally:
let obj = {};
obj[true] = "yes";
console.log(obj["true"]); // Output: "yes"
In this example, the boolean true is coerced to the string "true" when used as a property key.
However, implicit coercion can lead to subtle bugs, especially when mixing types or relying on truthy and falsy values. For example, adding a boolean to a number triggers numeric coercion rather than string coercion:
let flag = true; let result = flag + 5; console.log(result); // Output: 6
Here, true is coerced to the number 1, and the addition yields 6. This demonstrates that coercion depends on the operator and context.
To avoid confusion, it’s often better to be explicit about conversions. For example, when concatenating booleans with strings, use String() or toString() to make the intent clear and avoid unexpected coercion:
let flag = false; let message = "Flag is " + String(flag); console.log(message); // Output: "Flag is false"
Implicit type coercion with booleans leverages JavaScript’s dynamic typing rules, converting booleans to strings or numbers depending on the operation. Understanding these rules helps prevent bugs and write clearer code.
