Below is the example of the String() function.
- Example:
<script>functiongfg() {vargeek = Boolean(1);vargeeks = Boolean(0);varstring =String(geek) +"<br>"+String(geeks);document.write(string);}gfg();</script>chevron_rightfilter_none - 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.
<!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:

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

Supported Browsers:
- Google Chrome
- Firefox
- Internet Explorer
- Opera
- Safari
Recommended Posts:
- How to count string occurrence in string using JavaScript?
- JavaScript | Difference between String.slice and String.substring
- JavaScript | Check if a string is a valid JSON string
- JavaScript | Insert a string at position X of another string
- How to call function from it name stored in a string using JavaScript?
- JavaScript | Pass string parameter in onClick function
- How to create a function from a string in JavaScript ?
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | What is JavaScript ?
- JavaScript Course | Operators in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | JavaScript Prompt Example
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.


