What is console.log?

55

I see this in some JavaScript files:

console.log(algumaCoisa);
console.log("alguma coisa");

What is it good for and how does it work?

I'm trying to make a custom log() for a Userscript (see How can I log information without using alert in userscripts ) and I realized that I do not know what's behind console.log .

    
asked by anonymous 24.10.2014 / 19:21

4 answers

50

Console Object

console is effectively an object with several associated methods.

The console object provides access to the browser debug console. The operation of this object varies from browser to browser but there are certain methods that are seen as a standard. One of these methods is log() .

Log method

The log() method exists essentially to allow data to be sent to the browser debug console. Any information can be sent, usually for debugging code.

Example of the Firefox console:
Clickctrl+shift+ktocallitinFirefox.

Considerations

Asthisobjectvariesandthereisstillnostandardtogovernitsoperationandmethodology,therearesomeconsiderationstohave:

// Exemplo a enviar algo para a consola do navegador var s = "bubu"; console.log(s);

Potential problems:

  • The user may have turned off the console;
  • The browser may not have a Web console;
  • The method we are using may not work in a given browser.

In JavaScript, a runtime failure can result in a blocking of all other JavaScript. If for example we use console.log(X) when the console does not exist or the log() method does not exist, we will have an error in our code.

Alternative:

We can make use of a try...catch to try to send to the console and in case of failure to act otherwise:

function consola(s) {
    try { console.log(s); } catch (e) { alert(s); }
}

And so if it goes well, the information will be in the console, if it goes bad it will be emitted a alert() with the information that would go to the console.

Of course we can do nothing or send an Ajax call to a server with the error if it is critical and has to be registered "give to anyone who hurts."

/* Enviar algo para a consola do navegador com fallback 
 * fazendo uso da função já declarada conforme visto em cima
 */
var s = "bubu";
consola(s);

Consoles

As explained, there is not really a standard for the web console, so there are some references to the help pages for each one:

Note: Response to evolve within a few days as you finish compiling more relevant information on this subject.

    
24.10.2014 / 20:58
46

This writes any message in the browser LOG, which can be viewed at any time.

These messages can be used for various purposes:

  • How to use a library ... imagine that a function has become obsolete, and you want people to stop using the function, but without throwing an exception ... although in this case, other functions may be used: alert would be more recommended.

Other existing LOG methods:

  • console.warn
  • console.info
  • console.warn
  • console.error

They all serve to write messages, the difference between methods is precisely in the meaning of the message. Here's how the messages get with each of these methods:

(example: chrome console window)

    
24.10.2014 / 19:29
25

console.log is one of a number of other methods available for the browsers debug console .

List of other console methods . p>

console.log is used for the issuance of registration information in general. For example:

var algumObjeto = {
  str: "Algum texto",
  id: 5
};
console.log(algumObjeto);

Output:

({str:"Algum texto", id:5})

var algumObjeto = {
  str: "Algum texto",
  id: 5
};
console.log(algumObjeto);
Observar a saída no console do navegador!

You can also use string substitution and other arguments with this method. For example:

for (var i=0; i<5; i++) {
  console.log("Olá, %s. Você me chamou pela %dª vez.", "João", i+1);
}

Output:

Olá, João. Você me chamou pela 1ª vez.
Olá, João. Você me chamou pela 2ª vez.
Olá, João. Você me chamou pela 3ª vez.
Olá, João. Você me chamou pela 4ª vez.
Olá, João. Você me chamou pela 5ª vez.

for (var i = 0; i < 5; i++) {
  console.log("Olá, %s. Você me chamou pela %dª vez.", "João", i + 1);
}
<p>Observar a saída no console do navegador!</p>

Limitations and lack of support

This feature may not be available or active in the browser, so you should always test its existence before attempting to use it:

// verifica se o objeto window.console existe
if (window.console){
    window.console.log("Log isto");
}

Important notes

In addition to the situations mentioned above, old versions of IE, or < IE9 , do not present this method as previously presented (with the same name / signature) or do not present, as quoted in those references:

Source: MDN Console

    
24.10.2014 / 19:36
6

A way to debug your code. This command will show in the console of your browser what is written in the log function.

    
24.10.2014 / 19:24