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 functi…


This content originally appeared on DEV Community and was authored by Sham Gurav

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 !!!


This content originally appeared on DEV Community and was authored by Sham Gurav


Print Share Comment Cite Upload Translate Updates
APA

Sham Gurav | Sciencx (2022-04-12T15:04:19+00:00) Shorten The Console.log. Retrieved from https://www.scien.cx/2022/04/12/shorten-the-console-log/

MLA
" » Shorten The Console.log." Sham Gurav | Sciencx - Tuesday April 12, 2022, https://www.scien.cx/2022/04/12/shorten-the-console-log/
HARVARD
Sham Gurav | Sciencx Tuesday April 12, 2022 » Shorten The Console.log., viewed ,<https://www.scien.cx/2022/04/12/shorten-the-console-log/>
VANCOUVER
Sham Gurav | Sciencx - » Shorten The Console.log. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/12/shorten-the-console-log/
CHICAGO
" » Shorten The Console.log." Sham Gurav | Sciencx - Accessed . https://www.scien.cx/2022/04/12/shorten-the-console-log/
IEEE
" » Shorten The Console.log." Sham Gurav | Sciencx [Online]. Available: https://www.scien.cx/2022/04/12/shorten-the-console-log/. [Accessed: ]
rf:citation
» Shorten The Console.log | Sham Gurav | Sciencx | https://www.scien.cx/2022/04/12/shorten-the-console-log/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.