當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Javascript boolean.toString()用法及代碼示例


boolean.toString()是javascript中的內置函數,用於根據指定的布爾對象的值返回“true”或“false”字符串。
用法:

boolean.toString()

參數:此函數不接受任何單個參數。
返回值:它根據指定的布爾對象的值返回“true”或“false”字符串。

讓我們看一下此boolean.toString()函數上的一些JavaScript代碼:


  • 示例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

應用:

  • 此函數的應用程序將根據給定對象的值返回“true”或“false”字符串。
    讓我們看看此應用程序上的JavaScript程序:
    例:
    <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


相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript | boolean.toString() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。