This content originally appeared on DEV Community and was authored by Carlos Dubón
As developers we are always seeking innovative ways to boost our productivity 🚀. Today I bring you a clever hack to log stuff to the console faster 😎.
Let's begin!
The 3 most used methods of the console object in JavaScript are log
✏️, warn
⚠️ and error
❌:
console.log("Hello world 🌎"); // "Hello world 🌎"
console.warn("Hmm... 🤔"); // "Hmm... 🤔"
console.error("Oops! 🚨"); // "Oops! 🚨"
We can make use of object destructuring to extract ⛏️ these methods from the console object and bind them to constants:
const { log, error, warn } = console;
Now that we have extracted these methods from the console object we can call them from anywhere ✨:
const { log, error, warn } = console;
log("Hello world 🌎"); // "Hello world 🌎"
warn("Hmm... 🤔"); // "Hmm... 🤔"
error("Oops! 🚨"); // "Oops! 🚨"
If you plan to reuse this behavior across your entire app just add an export statement 📦:
// utils.js
export const { log, error, warn } = console;
// app.js
import { log } from "./utils.js";
log("Hello world 🌎"); // "Hello world 🌎"
And that's all folks. I hope you have found this post helpful and that I’ve made your development process a lil faster ⚡😉.
This content originally appeared on DEV Community and was authored by Carlos Dubón
