JavaScript String() Function

Below is the example of the String() function.

  • Example:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <script>
        function gfg() {
            var geek = Boolean(1);
            var geeks = Boolean(0);
      
            var string =
                String(geek) + "<br>" +
                String(geeks);
      
            document.write(string);
        }
        gfg();
    </script>

    chevron_right

    
    

  • Output:
    true
    false

The String() function is used to converts the value of an object to a string value.

Syntax:

String(object)

Parameter Values: This function accept a single parameter as mentioned above and described below:

  • object:This parameter contains a string which is converted to a string value.

Return Value: It returns a string value.



Program: Return Boolean’s string value.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 style="color:green;">
          Geeks for Geeks
        </h1>
        <button onclick="geek()">
          Press
        </button>
        <h4>
          Clicking on the 'Press' button
          will return the String Value.
        </h4>
        <p id="gfg"></p>
  
        <script>
            function geek() {
                var x = Boolean(1);
                var y = Boolean(0);
  
                var string =
                    String(x) + "<br>" +
                    String(y);
  
                document.getElementById(
                  "gfg").innerHTML = string;
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:
Image

Program 2: Return Date() object’s string value.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
    <center>
        <h1 style="color:green;">
          Geeks for Geeks
      </h1>
        <button onclick="geek()">
          Press
      </button>
        <h4>Clicking on the 'Press'
          button will return the 
          String Value.
      </h4>
  
        <p id="gfg"></p>
  
        <script>
            function geek() {
  
                var y = Date();
  
                var string =
                    String(y);
  
                document.getElementById(
                  "gfg").innerHTML = string;
            }
        </script>
    </center>
</body>
  
</html>

chevron_right


Output:
Image

Supported Browsers:

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

full-stack-img




My Personal Notes arrow_drop_up

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