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.


<title>Test Page</title>


<script language="javascript">

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

window.onload = run;



Test Page


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.