How can I print a JavaScript object?

I want to print the content of a JavaScript object in a string format like when we alert a variable.

The same formatted way I want to print an object.

share | improve this question
 
6  
Would you please reword your question? What do you mean by "formatted way"? As in, with rich formatting, like bold/italic/etc? –  Sasha Chedygov  Jun 5 '09 at 19:03

13 Answers

up vote 188 down vote accepted

If you want to print the object for debugging purposes, I suggest instead installing Firebug for Firefox and using the code:

console.log(obj)
share | improve this answer
 
18  
That function also works on Google Chrome when using the JavaScript Console (Shift+Control+J or Shift+Control+I, depending on the Chrome version). Also note that console.log(obj1, obj2) works very nicely, too, so you don't have to call console.log() for every object when logging multiple variables. Also, always remember to remove all such calls in production, as it will break browsers that do not implement it (such as Internet Explorer). –  Felix  Apr 22 '10 at 9:31
65  
This does not work it prints [object Object] –  Pete Herbert Penito  Dec 13 '10 at 21:17
30  
Yes it prints [object Object] but it has a little expando-toggly button beside it that lets you inspect the contents of the object. –  hughes  Jul 5 '11 at 13:46
7  
@hughes it actually can do both. i have an object i created with: var obj = { "foo" : false }; and another object that is being passed into a callback from a server, the one passed through the callback prints with the little arrow so you can open it up, the statically created one just prints [object Object] with no arrow. I'm trying to figure this out too, any other thoughts? –  benstraw  Jul 8 '11 at 0:08
3  
@Felix: Great point about breaking browsers such as IE. You can also use if(console && console.log) { console.log(obj1, obj2); } without worrying about breaking browsers that don't support console.log. –  chocojosh  Nov 22 '11 at 21:52
show 8 more comments

Use JSON.stringify(obj); Also this method works with nested objects.

share | improve this answer
 
2  
that's how I do it too. console.log only works, ehu, in a console! –  Stefano  Dec 17 '10 at 13:50
91  
For more readable output try JSON.stringify(obj, null, 4). This'll write it out as neatly indented text –  Ben Clayton Jun 5 '11 at 12:49
 
Beautiful output when shoved into a textarea –  Bob  Oct 12 '11 at 15:23
1  
Thanks, was trying to print out a complex, dynamic object to use as test data, and this saved me from having to create it manually. –  Jon  Nov 1 '11 at 5:14
 
This has my preference. –  James Poulson  Nov 16 '12 at 0:19
show 2 more comments
var output = '';
for (property in object) {
  output += property + ': ' + object[property]+'; ';
}
alert(output);
share | improve this answer
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值