Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Boolean
  6. toString()

Boolean.prototype.toString()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

ThetoString() method ofBoolean values returns a string representing the specified boolean value.

Try it

const flag1 = new Boolean(true);console.log(flag1.toString());// Expected output: "true"const flag2 = new Boolean(1);console.log(flag2.toString());// Expected output: "true"

Syntax

js
toString()

Parameters

None.

Return value

A string representing the specified boolean value.

Description

TheBoolean object overrides thetoString method ofObject; it does not inheritObject.prototype.toString(). ForBoolean values, thetoString method returns a string representation of the boolean value, which is either"true" or"false".

ThetoString() method requires itsthis value to be aBoolean primitive or wrapper object. It throws aTypeError for otherthis values without attempting to coerce them to boolean values.

BecauseBoolean doesn't have a[Symbol.toPrimitive]() method, JavaScript calls thetoString() method automatically when aBooleanobject is used in a context expecting a string, such as in atemplate literal. However, booleanprimitive values do not consult thetoString() method to becoerced to strings — rather, they are directly converted using the same algorithm as the initialtoString() implementation.

js
Boolean.prototype.toString = () => "Overridden";console.log(`${true}`); // "true"console.log(`${new Boolean(true)}`); // "Overridden"

Examples

Using toString()

js
const flag = new Boolean(true);console.log(flag.toString()); // "true"console.log(false.toString()); // "false"

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-boolean.prototype.tostring

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp