Access last logged value in Chrome console

JavascriptGoogle Chromeconsole.log

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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).

Solution 2 - Javascript

Just follow these steps:

  1. Click over the result with right button
  2. Save as global variable
  3. copy(temp1)

Solution 3 - Javascript

Solution 4 - Javascript

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.

Solution 5 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobin DrexlerView Question on Stackoverflow
Solution 1 - JavascriptsdrView Answer on Stackoverflow
Solution 2 - JavascriptAlessander FrançaView Answer on Stackoverflow
Solution 3 - JavascriptlaktakView Answer on Stackoverflow
Solution 4 - JavascriptsealocalView Answer on Stackoverflow
Solution 5 - JavascriptsealocalView Answer on Stackoverflow