What is console.log
? What is it used for in jQuery?

- 1
- 1

- 651
- 1
- 5
- 3
-
2A good question and completely unexpected that jQuery doesn't fix it or provide an alternative syntax - contrary to what the answerers here claim. Isn't one of the major points of jQuery to deal with this kind of browser incompatibility? – John Nov 16 '13 at 22:05
-
1Those are unrelated things. How does this have 52 up votes? – zoran404 Jul 08 '18 at 11:04
3 Answers
jQuery and console.log
are unrelated entities, although useful when used together.
If you use a browser's built-in dev tools, console.log
will log information about the object being passed to the log
function.
If the console is not active, logging will not work, and may break your script. Be certain to check that the console exists before logging:
if (window.console) console.log('foo');
The shortcut form of this might be seen instead:
window.console&&console.log('foo');
There are other useful debugging functions as well, such as debug
, dir
and error
. Firebug's wiki lists the available functions in the console api.

- 174,988
- 54
- 320
- 367
-
2
-
1But, whenever you wanted to add a log statement, you wouldn't need to do `window.console&&console.log('foo');`, right? Upon document load, couldn't you set console to window.console if console is undefined? – Kevin Meredith Jun 26 '13 at 20:31
-
1`window.console&&console.log('foo');` could sure use a little whitespace help! – David J. Dec 05 '13 at 06:09
It has nothing to do with jQuery, it's just a handy js method built into modern browsers.
Think of it as a handy alternative to debugging via window.alert()

- 7,146
- 1
- 23
- 25
it will print log messages in your developer console (firebug/webkit dev tools/ie dev tools)

- 41,224
- 16
- 95
- 126
-
3its a debugging command that print log messages. But keep in mind in IE make sure you activate the dev tools else it'll throw error. – KJYe.Name Feb 14 '11 at 13:59
-
2As an addition to @kjy112's message, console.log should NEVER be in production code! – the JinX Feb 14 '11 at 14:03
-
1FF dies too if firebug isn't up. but yeah, this is a debugging tool, not an actual logging tool. the other alternative is window.console = {}; window.console.log = function() {}; to put in a noop that will eat log messages. – Matt Briggs Feb 14 '11 at 23:27
-
1@the JinX Why in heck shouldn't it be in production code? If you have a bug in production, those logs will be very useful, and so long as only true errors are logged, it shouldn't cause performance issues. You don't want to be logging confidential info in production, of course, but that isn't a good reason to never log to console in production! – CorayThan Nov 21 '13 at 08:54
-
@CorayThan some browsers (IE) don't support the console.log method, thus halting javascript execution . . – the JinX Nov 21 '13 at 09:11
-
2@the JinX Alright, so straight up `console.log` shouldn't be used, but if you check to make sure the method exists before you use it, like zzzzBov suggests, it should be fine. – CorayThan Nov 21 '13 at 16:14