简体   繁体   中英

alert() return different from console.log()?

Should I be using alert() for debugging; or is there a time to use alert() vs. console.log()?

I see that alert() and console.log() can return different results. I assumed that they were similar functions but just returned in different places.

Back story: my boss likes to see alerts() during development but I can't get the object details in the alert (at least not easily).

But when I run the same request through console.log, I get the object and all of it's parameters.

Since alert could be shown to users, it tends to be literal-minded (just using toString) so a developer has a lot of control over what's shown to the user. Unlike alert, console is designed for developers, and thus tends to try to interpret a call so as to provide information that a developer would find useful: eg "[2, 3, 4]" is a lot more useful to a developer than "[object Object]". Alert should be the same in every browser; console's behavior could vary from browser to browser (including not being supported at all, as in IE).

try alert(JSON.stringify(yourObject)); (if your browser have json.stringify....)

alert() converts the object passed to it into a string using the object's toString() method. Unlike alert() , console.log() is not limited to displaying a simple string and can allow you to interact with the object passed to it, for example letting you inspect its properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM