Javascript To String: Formatting Data Using toString in Javascript

You can use toString() in javascript to format arrays and numbers as strings. The following HTML page contains embedded javascript that does exactly that.

<html>

<head>
<title>Test Page</title>

</style>

<script language="javascript">

function run() {
    var num1 = 72.6;
    
    var fruits = ['apple', 'orange', 'banana'];
    
    alert(num1.toString());
    alert(fruits.toString());
}

window.onload = run;

</script>

</head>

<body>
Test Page
</body>

</html>













If you're looking for a way to display the properties and methods of objects in javascript, toString() won't work (at least not at the moment). But you can easily iterate through the contents of an object and display everything using in. Check here for more details.

You can, however, use toString() to format numbers in the base of your choice.

The following example display a number in base-16 notation.

// A number.
var num = 111;
    
// Display in base 16.
alert(num.toString(16));