What is the use of console.log
?
Please explain how to use it in JavaScript, with a code example.
Advertisement
Answer
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:
JavaScript
x
5
1
$('#someButton').click(function() {
2
console.log('#someButton was clicked');
3
// do something
4
});
5
You’d then see #someButton was clicked
in Firebug’s “Console” tab (or another tool’s console — e.g. 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:
JavaScript
1
4
1
if (window.console && window.console.log) {
2
// console is available
3
}
4