Jump to content
w3schoon

Usage of console.log in jQuery

Recommended Posts

It's not jQuery, it's available in Javascript. It leaves a message in the browser's Javascript console. When you're debugging it's far more useful than alert() statements to show the data you're testing.

Share this post


Link to post
Share on other sites

Note that you should only use it for debugging, you shouldn't use it in production code. It doesn't exist in a couple browsers, and so using it will throw an error and stop your code from running.

Share this post


Link to post
Share on other sites

I've been able to get console.log to work (or at lease not throw errors) in all version of IE > 7, Opera, Chrome, FF, and Safari. You just need to define the object/method ahead of time in your JS.

Share this post


Link to post
Share on other sites

I'm quite sure jQuery does actually use console.log() to send messages to developers that are using it. If you're building a Javascript library or application you might want to keep console.log() statements. To make sure your program will work in browsers that don't support it you just declare an empty method if it doesn't already exist.

if(!console) {    console = {};    console.log = function() {};}

Share this post


Link to post
Share on other sites

Wow! That is amazing, I usually use alert() statements for debugging and I didn't know that console.log is more useful than alert() statements! May I know where I can find "browser's Javascript console" (if my browser is Google Chrome)? Thanks a lot! :D

Edited by w3schoon

Share this post


Link to post
Share on other sites

You can open developer tools by pressing F12. There's a "Console" tab on them where the messages will appear.

Share this post


Link to post
Share on other sites

what do you need to know? Just start logging stuff and you'll see. For example, logging arrays and objects will allow you expand/collapse their indexes/properties right in the console. Extrememely useful. Also, you can use it just like alert statements to leave trace logging to follow your code as it executes.

Edited by thescientist

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...