Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

George K.
George K.

Posted on

     

Pimping up your console.log output

With a bunch ofconsole.log statements sometimes it's a bit hard to track which one is printing out in the console and even withconsole.log('name of var ==>', var) approach sometimes it's easy to get lost, so how about we will add our own custom method toconsole and pimp it up a bit with styles?

Since we can do it with default.log method by doing:

console.log('%cbanana','font-family:helvetica;color:white;font-weight:bold;font-size:3em')

And of course it is annoying to type all of these every time we want to console.log something, we are going to add our custom method toconsole and call it instead.

First we will add a method to theconsole prototype and make it a function, taking the data to be printed out as an argument and returning the styled output:

console.__proto__.me=(what)=>{returnconsole.log(`%c${what}`,'font-family:helvetica;color:white;font-weight:bold;font-size:3em')}

Now we can print stuff by doing:

console.me('banana')

And the output would be this:

Alt Text

Hard to miss, huh?

Cheers!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Barcelona Code School – cutting edge curriculum and student-driven approach to help you become a developer or designer.Your choose. We teach.
  • Location
    Barcelona
  • Joined

More fromGeorge K.

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp