Last updated on October 11, 2022
Every JavaScript developer has to debug the code and log message in-browser debugging console, this is a very basic way to test the code, but JavaScript developer has to type this extra line in the code many times when coding this is nothing wrong, but you can reduce your development time and provide relief to your fingers by making it shorter and easier.
In this article, I have explained a simple and common method that is rarely used by front-end developers. As we use console.log()
like below
Console.log(“Testing something…”) Output: Testing something…
Console.log(4+4) Output: 8
Console.log(!true) Output: false
Let’s create a method to make it more shorter than console.log()
method
const log = (object) => console.log(object)
I created a simple function that works the same as console.log(). Its name is the log and that contains only three letters. Even you can make it still shorter like give it name only one letter
const l = (object) => console.log(object)
Writing this method will give you ease and comfort:
It is used to log the error message in the browser console. This method is helpful to print errors in the browser console, the error message will be highlighted with red color. Below is an example
doSomeThing() => ({
try {
const stuff = getStuff();
return do(stuff);
} catch (err) {
console.error(err);
}
})
This method is used to log the warning messages in the browser console. By default, warnings are highlighted with yellow color. Following is an example,
console.warn(‘write here warning message');
If you need to identify something regarding an element in the console you can use console.info in the browser debug console. Following is an example,
console.info(‘write here warning message');
If you need to print something in the table in browser debug console you can use below method
console.table(["Mango", "Banana", "Apple"]);
If you are testing a block or function you can use these both functions together time()
and consle.timeEnd()
console.time(“String”); console.timeEnd(“String”)
debug()
method is an alias for log()
, below is an example,
console.debug(“String”);