Skip to main content

Command Palette

Search for a command to run...

It's 2022, don't use the console.log(๐Ÿ˜Ž)

Published
โ€ข2 min read
It's 2022, don't use the console.log(๐Ÿ˜Ž)
D

I am 17 years old and a young passionate and self-taught frontend web developer and have an intention to become a successful developer. I usually write about JavaScript and Web Development and share some tips in the articles.

We as Javascript developers usually console.log() stuffs to test the output or just for fun. Even, I can bet that our (include me โœŒ๏ธ) first code was "Hello world" logged in the console.

console.log("Hello World!")

This piece of code has been nostalgic for all fellow JS developers. But now it's 2022, let's make it a little handy and comfortable for our fingers.

In this article, I have discussed a simple and common method that has rarely been used by developers.

Let's get started

As we know, we all use to log the data to console like this...

console.log("I love js") // I love js

console.log(4 + 4) // 8

console.log(!true)  // false

Let's work a little smarter and more efficient ๐Ÿ˜Ž

const log = (arg) => console.log(arg)

Here, we have created a function with a shorter name - log relative to console.log(), you can even use a shorter name, Ummm ๐Ÿค” something like this...

const l = (arg) => console.log(arg)

So, you might be wondering what's the benefit of writing code like this? Let's discuss the benefits.

Benefits

  • Keeps your code clean and slick
  • Improves readability
  • Relief to your fingers, don't have to write a long thing

comment more benefits if you can...

Let's test ๐Ÿš—

log("Hello world") // Hello world
log(4 + 4) // 8
log(!false) // true
log(Math.PI) // 3.141592653589793

Try yourself - fiddle

Conclusion

So, this was a quick tip to save your time and make your code look cleaner. Let me know in the comments if you will use this tip.

You can try the same thing for console. info(), console.warn(), console.error()


Feel free to reach me out via Twitter - @codewithsnowbit

๐ŸŒ Let's connect

Stay tuned for the next article. Stay safe and Happy Coding!

T

Hi, I am 15 years old and love writing in the next js. How can I reach you? If you are interested in the world's 1st self community website, you can message me on discord -- https://discord.gg/4tWJPc7c3E

3
B

This never crossed my mind. Wow! So cool. Thanks for sharing this. I'll definitely start using it.

3
D

You're welcome Benjamin Semah

Happy coding!

A

I use this one.

const debugApp = false;
const debugMsg = (msg) => debugApp && console.log(msg)
2
R

Great tip given in general. This is called working smart. Although for all the devs out there I would suggest you to use debugger because the habit of writing console.log() will become very confusing to find any error especially when your project scales up.

4
V

I was getting annoyed by having to type the full thing so many times, especially when debugging JS code, and my solution was to add a user code snippet to VSCode that stores it. So now I just press cl, and it already suggests the full method. The beauty of it is that you can create snippets for literally anything you want!

1
J

One thing I like to do is write a "logger" module. Just a set of thin wrappers over console.log(). But I can for example, use it as a request logger middleware in Express, an error logger, and so on :)

1
M

Thanks for the advice. I will refactor my project Selenidium Element Inspector๐Ÿ˜Š: https://github.com/mszeles/selenideium-element-inspector

4
D

You're welcome Miki Szeles

3
M

The pleasure is minรฉ. ๐Ÿ˜ŠSnowBit

3

More from this blog

D

Dhairya Shah

119 posts