imtiaz imtiaz - 1 month ago 6
Javascript Question

how to get the output of object in javascript

when I add some object in editor & when I go to the browser to see the output, it only shows [object object] .Can any one tell me how to see the output of object in browsers

enter code here

// alert('Hello world ');

var contacts= new Array();


var add = function(firstName,lastName,phoneNumber,email)
{

this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber =phoneNumber;
this.email = email;
};

contacts[contacts.length] = new add("imtiaz","ahmed","01794705874","imtiaz@gmail.com");

contacts[contacts.length] = new add("johny","bob","998978788","bob@gmail.com");


document.write( "</br>" + contacts);

Answer

Lets say if you have an object like this.

 var Somebody = {
 name: "John Smith",
 age: 120};

To print it in the browser you can use JSON like this.

alert(JSON.stringify(Somebody, null, 3));