JavaScript Boolean toString() Method

Below is the example of Boolean toString() method.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

  • Output:
    true

The boolean.toString() method is used to return a string either “true” or “false” depending upon the value of the specified boolean object.

Syntax:

boolean.toString()

Parameter: This method does not accept any parameter.
Return Values: It returns a string either “true” or “false” depending upon the value of the specified boolean object.

More codes for the above method are as follows:



  • Example 1:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    Output:

    true
  • Example 2:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    Output:

    true
  • Example 3:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    Output:

    true
  • Example 4:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    Output:

    true
  • Example 5:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    Output:

    true
  • Example 6:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    Output:

    false

Errors and Exceptions:

  • Example 1: Here the value as geeksforgeeks gives error because this value is not defined only true and false has been predefined.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    Output:

    Error: geeksforgeeks is not defined
  • Example 2: Here complex number can not be taken as the parameter only integer values and string can be taken as the parameter that is why it returns error.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <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>

    chevron_right

    
    

    Output:

    Error: Invalid or unexpected token

Supported Browsers: The browsers supported by JavaScript Boolean toString() Method are listed below:

  • Google Chrome
  • Internet Explorer
  • Mozilla Firefox
  • Safari
  • Opera

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.