Shortcut to console.log

Sometimes, I'm just lazy to type '˜console.log'˜ to print some data on the Firebug console. I often wanted to have a simple *echo() *function that spits whatever it gets to the console.

So, in every single JavaScript project I work on, I used to have this utility method:

function echo(obj){
    echo('Hello World');

It worked well. But there was a problem. The printed message on the console will always show the line number of *console.log  *as the source line number instead of showing the where exactly the *echo() *function is invoked.

There's another way to solve this. Instead of creating a new *echo() *method, I just need to bind it to the existing *console.log() *to a variable and the logging works like a charm. Like this,

window.echo = console.log.bind(console);
    window.oops = console.error.bind(console);
    echo('hello world');
    oops('I did it again');

The methods became shorter and the correct line numbers printed on the console!

Veerasundar profile photo

Written by Veerasundar

Veerasundar is a Software Engineer, currently working at Salesforce as a Lead Member of Technical Staff. He has over 16 years of industry experience, building consumer facing web applications. He lives in Chennai, India.

Contact Veerasundar: Email or Twitter