以下是布爾toString()方法的示例。
- 例:
<script> // Here Boolean object obj // is created for the value 27 var obj = new Boolean(27); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
- 輸出:
true
boolean.toString()方法用於根據指定的布爾對象的值返回“true”或“false”字符串。
用法:
boolean.toString()
參數:此方法不接受任何參數。
返回值:根據指定的布爾對象的值,它返回一個字符串“true”或“false”。
上述方法的更多代碼如下:
- 範例1:
<script> // Here Boolean object obj is // created for the value true. var obj = new Boolean(true); // Here boolean.toString() function // is used for the created object obj. document.write(obj.toString()); </script>
輸出:
true
- 範例2:
<script> // Here Boolean object obj is // created for the value 1. var obj = new Boolean(1); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
輸出:
true
- 範例3:
<script> // Here Boolean object obj is // created for the value -1. var obj = new Boolean(-1); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
輸出:
true
- 範例4:
<script> // Here Boolean object obj is // created for the value 1.2 var obj = new Boolean(1.2); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
輸出:
true
- 範例5:
<script> // Here Boolean object obj is created // for the value as string "gfg" var obj = new Boolean("gfg"); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
輸出:
true
- 範例6:
<script> // Here Boolean object obj is created // for the value false. var obj = new Boolean(false); // Here boolean.toString() function is // used for the created object obj. document.write(obj.toString()); </script>
輸出:
false
錯誤和異常:
- 範例1:這裏的值geeksforgeeks會給出錯誤,因為不僅未定義該值,而且還預定義了false。
<script> // Here Boolean object obj is created // for the value geeksforgeeks. var obj = new Boolean(geeksforgeeks); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); </script>
輸出:
Error:geeksforgeeks is not defined
- 範例2:此處不能將複數作為參數,而隻能將整數值和字符串作為參數,這就是它返回錯誤的原因。
<script> // Here Boolean object obj is created for the // value such as complex number 1+2i var obj = new Boolean(1 + 2i); // Here boolean.toString() function is // used for the created object obj. console.log(obj.toString()); </script>
輸出:
Error:Invalid or unexpected token
支持的瀏覽器:下麵列出了JavaScript布爾toString()方法支持的瀏覽器:
- 穀歌瀏覽器
- IE瀏覽器
- 火狐瀏覽器
- Safari
- Opera
相關用法
- Javascript RegExp toString()用法及代碼示例
- Javascript toString()用法及代碼示例
- JavaScript Array toString()用法及代碼示例
- Javascript date.toString()用法及代碼示例
- Javascript string.toString()用法及代碼示例
- JavaScript BigInt.prototype.toString()用法及代碼示例
- JavaScript Error.prototype.toString()用法及代碼示例
- JavaScript Boolean valueOf()用法及代碼示例
- Javascript typedArray.toString()用法及代碼示例
- Javascript symbol.toString()用法及代碼示例
- javascript Boolean constructor用法及代碼示例
- Javascript string轉boolean用法及代碼示例
- Node.js Buffer.toString()用法及代碼示例
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript Boolean toString() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。