user1432124
user1432124

Reputation:

What is the need of console in JavaScript?

Someone told me, after using console.log() method, I would not need alert boxes and prepared some code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script>
function cool()
{console.log("Message");}</script>
</head>

<body onload="cool();">
</body>
</html>

But it is not making the JavaScript stop running like a alert box would. It is also writing "Message" to the Firebug window.

Upvotes: 1

Views: 237

Answers (3)

jamesmortensen
jamesmortensen

Reputation: 34038

There are some browsers, like IE7, where some of the console methods aren't defined. If you're a frequent user of console.debug, console.info, and other methods that may not be available in IE7 and other browsers, you can handle that condition by pasting this code before any other JavaScript runs on your page:

// override loggers to avoid throwing errors
if(window.console == null || window.console == undefined || !window.console) {
           console = { log: function() {}, info: function() {}, warn: function() {}, error: function() {}, trace: function() {}, debug: function() {} };
           var fbscript = document.createElement("script");
           fbscript.src = "https://getfirebug.com/firebug-lite-beta.js";
           fbscript.setAttribute("type","text/javascript");
           document.getElementsByTagName("head")[0].appendChild(fbscript);
} else if(!console.debug) {
         console.debug = function(text) { if(console.log) console.log("D: "+text); };
}

This first checks to see if window.console is null or undefined. If it is, then it defines console, console.log, console.info, etc as empty functions. This prevents them from causing errors in browsers that don't support them, and it prevents you from needlessly removing what are otherwise very helpful breadcrumbs when there are bugs or other issues in the code.

The other thing this does is inputs Firebug Lite if console is not found. This is great in browsers that don't have a native debugger.

The advantage of console log statements over alerts is that the log statements don't stop execution of the code. When testing features or debugging code where there are several events firing simultaneously, having alerts in your code could mask some unknown bugs that you either don't discover until you are about to deploy and remove your alerts, or worse, they don't get discovered until after the code hits production.

As far as the audience goes, I don't know too many nontechnical people that know what Firebug is or the Chrome Debugger. Thus, it appears likely that web developers were indeed the intended targets of these features. That's not to say they can't be used in other capacities. For instance, I use the chrome debugger to remove annoying sidebar ads when I'm watching videos online.

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

How it works on the Client browser which has not firebog.

It doesn't work :-) Actually FireBug is not a requirement. Modern browsers such as Google Chrome for example have the console.log method implemented and the output goes into their console. For legacy browsers it doesn't work because this method is not defined.

Was it made only for web developing purpose?

Yes, primarily for debugging.

Upvotes: 2

Oded
Oded

Reputation: 498904

Logging messages does just that.

How it works on the Client browser which has not firebog.

It outputs to whatever javascript console the browser has defined. FireBug intercepts this but is not needed.

Does it work on all browsers with same syntax?

Yes.

Was it made only for web developing purpose?

For debugging javascript.

Upvotes: 1

Related Questions