The String()
function converts the value of an object to a string.
String |
Yes | Yes | Yes | Yes | Yes |
var v = String(object)
Parameter | Description |
---|---|
object | Required. A JavaScript object |
The following code shows how to convert different objects to strings.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from w w w . ja v a2s . c o m-->
<p id="demo"></p>
<script>
function myFunction() {
var x1 = Boolean(0);
var x2 = Boolean(1);
var x3 = new Date();
var x4 = "12345";
var x5 = 12345;
var res =
String(x1) + "<br>" +
String(x2) + "<br>" +
String(x3) + "<br>" +
String(x4) + "<br>" +
String(x5);
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
The code above is rendered as follows: