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 2015年7月.
toString()
メソッドは、指定された Boolean オブジェクトを表す文字列を返します。
試してみましょう
const flag1 = new Boolean(true);
console.log(flag1.toString());
// Expected output: "true"
const flag2 = new Boolean(1);
console.log(flag2.toString());
// Expected output: "true"
構文
js
toString();
返値
指定された Boolean
オブジェクトを表す文字列です。
解説
Boolean
オブジェクトは Object
オブジェクトの toString
メソッドを上書きしており、 Object.prototype.toString()
を継承していません。 Boolean
オブジェクトでは、 toString
メソッドはオブジェクトを文字列で表したものを返します。
JavaScript は toString()
メソッドを、 Boolean
が文字列値として表現されるべき場合や、 Boolean
が文字列の結合で参照されたときに自動的に呼び出します。
Boolean
オブジェクトと論理値では、組み込みの toString
メソッドはその Boolean
オブジェクトの値によって "true
" か "false
" の文字列を返します。
例
>toString
の使用
以下のコードでは、flag.toString()
は "true
" を返します。
js
var flag = new Boolean(true);
var myVar = flag.toString();
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification> # sec-boolean.prototype.tostring> |
ブラウザーの互換性
Loading…