Javascript Reference - JavaScript String() Function








The String() function converts the value of an object to a string.

Browser Support

String Yes Yes Yes Yes Yes

Syntax

var v = String(object)

Parameter Values

Parameter Description
object Required. A JavaScript object




Example

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: