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!
Top comments (0)