A faster way to write console.log ⚡

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 …

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 😎.

To the moon

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 ⚡😉.

Dog over turtle


Print Share Comment Cite Upload Translate
APA
Carlos Dubón | Sciencx (2024-03-28T10:56:31+00:00) » A faster way to write console.log ⚡. Retrieved from https://www.scien.cx/2021/10/18/a-faster-way-to-write-console-log-%e2%9a%a1/.
MLA
" » A faster way to write console.log ⚡." Carlos Dubón | Sciencx - Monday October 18, 2021, https://www.scien.cx/2021/10/18/a-faster-way-to-write-console-log-%e2%9a%a1/
HARVARD
Carlos Dubón | Sciencx Monday October 18, 2021 » A faster way to write console.log ⚡., viewed 2024-03-28T10:56:31+00:00,<https://www.scien.cx/2021/10/18/a-faster-way-to-write-console-log-%e2%9a%a1/>
VANCOUVER
Carlos Dubón | Sciencx - » A faster way to write console.log ⚡. [Internet]. [Accessed 2024-03-28T10:56:31+00:00]. Available from: https://www.scien.cx/2021/10/18/a-faster-way-to-write-console-log-%e2%9a%a1/
CHICAGO
" » A faster way to write console.log ⚡." Carlos Dubón | Sciencx - Accessed 2024-03-28T10:56:31+00:00. https://www.scien.cx/2021/10/18/a-faster-way-to-write-console-log-%e2%9a%a1/
IEEE
" » A faster way to write console.log ⚡." Carlos Dubón | Sciencx [Online]. Available: https://www.scien.cx/2021/10/18/a-faster-way-to-write-console-log-%e2%9a%a1/. [Accessed: 2024-03-28T10:56:31+00:00]
rf:citation
» A faster way to write console.log ⚡ | Carlos Dubón | Sciencx | https://www.scien.cx/2021/10/18/a-faster-way-to-write-console-log-%e2%9a%a1/ | 2024-03-28T10:56:31+00:00
https://github.com/addpipe/simple-recorderjs-demo