85

When I evaluate an expression directly in the Chrome Console like

1 + 1

then I can reference to the evaluated value using

$_

enter image description here

However, I can't access the value with $_, when the value is a result of a console.log, coming from inside of my application, instead of an expression I typed directly into the console.

enter image description here

Is there a way to access the last evaluated expression, regardless where it came from?

Robin Drexler
  • 4,307
  • 3
  • 25
  • 28

5 Answers5

76

After it's been logged to the console, you can right click on it and get an option to Store as global object. Clicking this will define a new variable like 'temp1' which will point to the variable. Here's a video of it in action (not mine).

Karambit
  • 317
  • 4
  • 11
sdr
  • 1,625
  • 1
  • 15
  • 16
14

Just follow these steps:

  1. Click over the result with right button
  2. Save as global variable
  3. copy(temp1)
Alessander França
  • 2,697
  • 2
  • 29
  • 52
3

You can only copy & paste.

See all available commands and shortcuts:

https://developers.google.com/chrome-developer-tools/docs/commandline-api https://developers.google.com/chrome-developer-tools/docs/shortcuts

laktak
  • 57,064
  • 17
  • 134
  • 164
  • Is c&p really an option, when the evaluated expression is e.g. an object? – Robin Drexler Apr 09 '13 at 07:47
  • 1
    console.log() outputs a string - so no, you can't get an object from it. You should use the debugger for that. – laktak Apr 09 '13 at 07:53
  • 2
    It is not true that `console.log()` outputs a string, at least in Chrome. It accepts an arbitrary object and obviously retains a reference to the actual object because it allows you to inspect attributes of the object and its subobjects to arbitrary depth. To test this, evaluate something like `console.log((function(){var o={};o.recurse=o;return o;})());` in the console. – user4815162342 Jan 20 '14 at 07:44
  • @user4815162342 nice find - however that still allows you only to view the object, not to assign it. – laktak Jan 20 '14 at 14:10
  • @hcris It also gives one hope that it might be somehow possible to get a hold of the object. :) – user4815162342 Jan 20 '14 at 14:19
1

A work-around for this is to define a variable in the global namespace. Presumably, your console.log(local_variable) is inside a function.

<script>
  var global_variable = null;

  function some_function() {
    var local_variable = 0;
    global_variable = local_variable;
    console.log(local_variable);
  }
</script>

Here, when some_function() is called, your local_variable will be logged, but you can then type global_variable in your console to get its value quickly and work with it.

sealocal
  • 10,897
  • 3
  • 37
  • 50
1

You could access any evaluated expression at any point in execution with Chrome's DevTools by setting breakpoints.

Your logged expression should have a clickable line number in the console - follow the link, then set a breakpoint on the line of code (which should be your console.log).

Full guide on breakpoints:
https://developers.google.com/web/tools/chrome-devtools/javascript/breakpoints

sealocal
  • 10,897
  • 3
  • 37
  • 50