What is console.log?

What is the use of console.log ?

Please explain how to use it in JavaScript, with a code example.


It's not a jQuery feature but a feature for debugging purposes. You can for instance log something to the console when something happens. For instance:

$('#someButton').click(function() {
  console.log('#someButton was clicked');
  // do something
});

You'd then see #someButton was clicked in Firebug's “Console” tab (or another tool's console — eg Chrome's Web Inspector) when you would click the button.

For some reasons, the console object could be unavailable. Then you could check if it is - this is useful as you don't have to remove your debugging code when you deploy to production:

if (window.console && window.console.log) {
  // console is available
}

Places you can view the console! Just to have them all in one answer.

Firefox

https://getfirebug.com/

Also Firefox's built in developer tools Ctrl+Shift+J (Tools > Web Developer > Error Console) (but Firebug is much better; use Firebug)

https://developer.mozilla.org/en-US/docs/Tools

Safari and Chrome

Basically the same.

https://developers.google.com/web/tools/chrome-devtools/

https://developer.apple.com/safari/tools/

Internet Explorer

Don't forget you can use compatibility modes to debug IE7 and IE8 in IE9 or IE10

https://msdn.microsoft.com/en-us/library/gg589507(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/dd565628(v=vs.85).aspx

If you must access the console in IE6 for IE7 use the Firebug Lite bookmarklet

https://getfirebug.com/firebuglite/ look for stable bookmarklet

https://en.wikipedia.org/wiki/Bookmarklet

Opera

https://www.opera.com/dragonfly/

iOS

Works for all iPhones, iPod touch and iPads.

https://developer.apple.com/library/content/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/
(archive link for Debugging Web Content on iOS)

Now with iOS 6 you can view the console through Safari in OS X if you plug in your device. Or you can do so with the emulator, simply open a Safari browser window and go to the "Develop" tab. There you will find options to get the Safari inspector to communicate with your device.

Windows Phone, Android

Both of these have no console built in and no bookmarklet ability. So we use https://jsconsole.com/ type :listen and it will give you a script tag to place in your HTML. From then on you can view your console inside the jsconsole website.

iOS and Android

You can also use http://html.adobe.com/edge/inspect/ to access web inspector tools and the console on any device using their convenient browser plugin.


Older browser problems

Lastly older browsers (thanks again Microsoft) will crash if you use console.log in your code and not have the developer tools open at the same time. Luckily its an easy fix. Simple use the below code snippet at the top of your code and good old IE should leave you alone:

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

This checks to see if the console is present, and if not it sets it to an object with a blank function called log . This way window.console and window.console.log is never truly undefined.


You can view any messages logged to the console if you use a tool such as Firebug to inspect your code. Let's say you do this:

console.log('Testing console');

When you access the console in Firebug (or whichever tool you decide to use to inspect your code), you will see whatever message you told the function to log. This is particularly useful when you want to see if a function is executing, or if a variable is being passed/assigned properly. It's actually rather valuable for figuring out just what went wrong with your code.

链接地址: http://www.djcxy.com/p/2974.html

上一篇: String.slice和String.substring有什么区别?

下一篇: 什么是console.log?