String() Function - Javascript String

Javascript examples for String:constructor


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

It returns the same value as toString() of the individual objects.

Parameter Values

Parameter Description
objectRequired. A JavaScript object

The following code shows how to Convert different objects to strings:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

function myFunction() {/*from ww  w .  ja  v a2  s .c o m*/
    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>" +
    document.getElementById("demo").innerHTML = res;


Related Tutorials