Remove all console.log() in one go!

Keep your code clean

No matter how senior you are, you are definitely going to use the evergreen console.log() to debug your JavaScript or typescript project. There is no problem in doing so (some JavaScript veterans might disagree), but the problem arises when you forget removing them and push your code for:

  1. Code review (by raising a pull request)
  2. Production

Believe me, finding a console.log() in production ready code is a sin and if you are using console.log() to debug and you forget to remove it, its like littering all over the code-base.

Do not litter.

Therefore, if you are using VS Code for development or any other IDE which supports searching via Regular Expressions (regex) then use this technique to remove all the console.log() in one go.

In VS Code:

  1. Open the global search and replace by pressing Ctrl+Shift+H (in Windows) or Cmd+Shift+H (in Mac).
  2. Search for the regex — console.log.*$
  3. Press Alt+R to select the Regular Expression mode or click on the “Use Regular Expression icon” and hit Enter. If there are console.log() in your project, the sidebar will get populate with them.
  4. Leave the “Replace” input empty
  5. Click on “Replace All”
  6. Enjoy!
Use regular expression
Click on Replace All

Make a habit to:

  1. Lint your code before you commit
  2. Remove all unnecessary console.log() — DO NOT LITTER.

This will help you to keep the code clean and also ask your fellow friends/colleagues not to litter!

Turbo Console Log

Turbo Console Log

Turbo Console Log is a VS code extension which comes handy while developing and debugging. You appreciate the sheer power of this extension when you are deep into debugging a particular function flow and you are repeatedly adding and removing console.log() as breakpoints.

Adding meaningful log messages:

  • Selecting the variable which is the subject of the debugging
  • Pressing Ctrl + Alt + L

Not only this you can do some magical stuff with the console.log() messages inserted by the extension (not the ones you did manually) like:

  1. Alt + Shift + C : Comment all log messages, inserted by the extension, from the current document
  2. Alt + Shift + U : Un-comment all log messages, inserted by the extension, from the current document
  3. Alt + Shift + D : Delete all log messages, inserted by the extension, from the current document

If you find this helpful, do share this with your colleagues and dev-friends.

P.S Originally posted on my blog!

--

--

--

Programmer and a voracious reader who writes about web development | adityatyagi.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

GTM 常用的 10 個 JavaScript Tricks

Web3Js setup in ReactJs Project.

Introducing F2

Building a Nodemon Clone from Scratch in 20 lines of JavaScript

nodemon clone in node.js

JavaScript: Working With Conditionals

How to Never Repeat PropTypes Declarations Again

Blazor Form Validation Mechanics Overview

Unsplash API with React Part 4

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditya Tyagi

Aditya Tyagi

Programmer and a voracious reader who writes about web development | adityatyagi.com

More from Medium

Typescript Generics Explained Easily

Advanced TypeScript: Type-Level Nested Object Paths

Example of nested object paths in typescript

30 Day Typescript Type Challenge — Challenge 10

Client Side Gzip Compression using Webpack in React