DEV Community

Uriel Bitton
Uriel Bitton

Posted on

2 1

Customizing console.log in JavaScript

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

Enter fullscreen mode Exit fullscreen mode

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!

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay