DEV Community

Sham Gurav
Sham Gurav

Posted on

3 2

Shorten The Console.log

Tired of writing console.log() again and again ??? the the following JS tip is for you .

You can shorten the log() function with the help of Function.prototype.bind .

Since ES5, all functions have a bind method that allows one to create a new function specifying the context.

Use bind method to pre-define the context execution to always point to console. This way, no matter how the function is invoked, we are instructing the JavaScript engine to always set the context execution to console.

const C = console.log.bind(console);

C("Downloaded Successfully !!!");
// Downloaded Successfully !!!

C("Code Executed Successfully !!!");
// Code Executed Successfully !!!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay