Assign console.log value to a variable

You could override standard console.log() function with your own, adding the behaviour you need:

console.oldLog = console.log;

console.log = function(value)
{
    console.oldLog(value);
    window.$log = value;
};

// Usage

console.log('hello');

$log // Has 'hello' in it

This way, you don't have to change your existing logging code. You could also extend it adding an array and storing the whole history of printed objects/values.


In Chrome developer tools, you may access last item by $_:

> 1+1;
  2
> $_
  2

If you want to do this to an object that has been already logged (one time thing), chrome console offers a good solution.

Hover over the printed object in the console, right click, then click on "Store as Global Variable". Chrome will assign it to a temporary var name for you which you can use in the console.

enter image description here