当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。