Console object
The console
is effectively an object with several associated methods.
The object console
provides access to the browser debugging console. The operation of this object varies from browser to browser but there are certain methods that are viewed as a standard. One of these methods is the log()
.
Log method
The method log()
exists essentially to allow sending data to the browser debug console. Any information can be sent, usually for the purpose of debugging code.
Example of Firefox console:
Priming Ctrl + shift + k to call it in Firefox.
Considerations
As this object varies and there is still no standard governing the functioning and methodology of it, there are some considerations to have:
// Exemplo a enviar algo para a consola do navegador
var s = "bubu";
console.log(s);
Potential problems:
- 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 particular browser.
In Javascript, an execution failure can result in a blocking of all other Javascript. If for example we make use of the console.log(X)
when console does not exist or method log()
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 on the console, if it goes wrong, a alert()
with 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 logged "hurt who it 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 exactly a standard for the web console, so there are some references to each one’s help pages:
Note: Response to evolve within a few days as soon as you finish compiling more relevant information on this subject.
Some tips: You can use Colors to have a Better view of: console.log('%c Sample Text', 'color:green;'); Or add some VAR in the text using: console.log(`Sample ${variable}`, 'color:green;'); When you have some objects, you can show using some table: https://www.w3schools.com/jsref/met_console_table.asp
– Gilberto B. Terra Jr.