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


JavaScript Boolean valueOf()用法及代碼示例


以下是布爾valueOf()方法的示例。

  • 例:
    <script> 
      // Here Boolean object obj  
      // is created for the value 27 
      var obj = new Boolean(27); 
      
      // Here boolean.valueOf() function is  
      // used for the created object obj. 
      document.write(obj.valueOf());
  • 輸出:
    true

boolean.valueOf()方法用於根據指定的布爾對象的值返回布爾值“true”或“false”。

用法:

boolean.valueOf()

參數:此方法不接受任何參數。

返回值:它根據指定的布爾對象的值返回布爾值“true”或“false”。



上述方法的更多代碼如下:

  • 程序1:
    <script> 
      // Here Boolean object obj is created  
      // for the value true. 
      var obj = new Boolean(true); 
      
      // Here boolean.valueOf() function is  
      // used for the created object obj. 
      document.write(obj.valueOf()); 
    </script>

    輸出:

    true
  • 程序2:
    <script> 
      // Here Boolean object obj is 
      // created for the value 1. 
      var obj = new Boolean(1); 
      
      // Here boolean.valueOf() function  
      // is used for the created object obj. 
      document.write(obj.valueOf()); 
    </script>

    輸出:

    true
  • 程序3:
    <script> 
      // Here Boolean object obj is  
      // created for the value -1. 
      var obj = new Boolean(-1); 
      
      // Here boolean.valueOf() function 
      // is used for the created object obj. 
      document.write(obj.valueOf()); 
    </script>

    輸出:

    true
  • 程序4:
    <script> 
      // Here Boolean object obj is  
      // created for the value 1.2 
      var obj = new Boolean(1.2); 
      
      // Here boolean.valueOf() function  
      // is used for the created object obj. 
      document.write(obj.valueOf()); 
    </script>

    輸出:

    true
  • 程序5:
    <script> 
      // Here Boolean object obj is  
      // created for the value as string "gfg" 
      var obj = new Boolean("gfg"); 
      
      // Here boolean.valueOf() function is  
      // used for the created object obj. 
      document.write(obj.valueOf()); 
    </script>

    輸出:

    true
  • 程序6:
    <script> 
      // Here Boolean object obj is created for the value false. 
      var obj = new Boolean(false); 
      
      // Here boolean.valueOf() function is 
      // used for the created object obj. 
      document.write(obj.valueOf()); 
    </script>

    輸出:

    false
  • 程序7:
    <script> 
      // Here Boolean object obj is created  
      // for the value zero (0) 
      var obj = new Boolean(0); 
      
      // Here boolean.valueOf() function is  
      // used for the created object obj. 
      document.write(obj.valueOf()); 
    </script>

    輸出:

    false

    錯誤和異常:在控製台上檢查此程序。

  • 程序1:這裏的值geeksforgeeks會產生錯誤,因為不僅未定義該值,而且還預定義了false。
    <script> 
      // Here Boolean object obj is created  
      // for the value geeksforgeeks. 
      var obj = new Boolean(geeksforgeeks); 
      
      // Here boolean.valueOf() function is 
      // used for the created object obj. 
      console.log(obj.valueOf()); 
    </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.valueOf() function is 
      // used for the created object obj. 
      console.log(obj.valueOf());

    輸出:

    Error:Invalid or unexpected token

支持的瀏覽器:下麵列出了JavaScript布爾valueOf()方法支持的瀏覽器:

  • 穀歌瀏覽器
  • IE瀏覽器
  • 火狐瀏覽器
  • Safari
  • Opera




相關用法


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