loading...

Customizing console.log in JavaScript

urielbitton profile image Uriel Bitton ・1 min read

How many of you use console.log to understand what is going on in your code, output messages or debug some code? The answer is everyone.
In fact, most developers use console.log every few minutes when debugging code. You probably know this amd do so yourself, but did you know that you can customize it to be its own variable?

Let's explain.
We can assign the function of console.log to a variable we want and then use that variable as our method to log output to our console. What's the purpose? Well, instead of having to type console.log everytime we want to output to the console we can just use our shorter vsriable.

Let's see an example

const msg = console.log
msg("Hello world") //hello world

This helps us use msg() which is shorter and faster to type than console.log

One thing to note is that it is highly recommended to declare our variable as a const and not as let or var. This is due to the simple reason that if 1000 lines down our code we forget we have assigned console.log to msg, we could unintentionally change its value, so by declaring with const we protect ourselves from this error.

Try the code and let me know what you think innthe comments!

Posted on by:

urielbitton profile

Uriel Bitton

@urielbitton

Hi i'm an avid web and app developer in search for creativity and always going after new ideas. Developer of Reactor JS

Discussion

pic
Editor guide