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.
<!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> |
Output:
Before clicking on the button:

After clicking on the button:

Example-2: Return Date() object’s string value.
<!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> |
Output:
Before clicking on the button:

After clicking on the button:

Browser Support: The listed browsers support String() function
- Google Chrome
- Firefox
- Internet Explorer
- Opera
- Safari
Recommended Posts:
- How to call function from it name stored in a string using JavaScript?
- JavaScript | Pass string parameter in onClick function
- JavaScript | Difference between String.slice and String.substring
- JavaScript | Check if a string is a valid JSON string
- How to count string occurrence in string using JavaScript?
- JavaScript | Insert a string at position X of another string
- JavaScript | string.length
- JavaScript | string.substring()
- JavaScript | string.codePointAt()
- JavaScript | string.toString()
- JavaScript | String.fromCodePoint()
- JavaScript | String toUpperCase()
- JavaScript | String toLowerCase()
- JavaScript | String split()
- JavaScript | String indexOf()
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.



