JavaScript | String() Function

It converts the value of an object to a string value.

Syntax:

String(object)

Parameter Values:

  • object: An object created in javascript.

Return Value: It returns a string value.

Example-1: 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:
Before clicking on the button:
Image
After clicking on the button:
Image

Example-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:
Before clicking on the button:
Image

After clicking on the button:
Image

Browser Support: The listed browsers support String() function

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


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.