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