JavaScript

Boolean.prototype.toString()メソッド

 編集:2022.08.04 by T.A.

toString() メソッドは、指定された Boolean オブジェクトを表す文字列を返します。

構文
Boolean.toString()
Boolean Boolean
返値

指定された Boolean オブジェクトを表す文字列です。

解説

Boolean オブジェクトは Object オブジェクトの toString メソッドを上書きしており、Object.prototype.toString() を継承していません。Boolean オブジェクトでは、toString メソッドはオブジェクトを文字列で表したものを返します。

JavaScript は toString() メソッドを、Boolean が文字列値として表現されるべき場合や、Boolean が文字列の結合で参照されたときに自動的に呼び出します。

Boolean オブジェクトと論理値では、組み込みの toString メソッドはその Boolean オブジェクトの値によって "true" か "false" の文字列を返します。

toString の使用

以下のコードでは、flag.toString() は "true" を返します。

var flag = new Boolean(true);
var myVar = flag.toString();

テスト