Access last logged value in Chrome console
JavascriptGoogle Chromeconsole.logJavascript Problem Overview
When I evaluate an expression directly in the Chrome Console like
1 + 1
then I can reference to the evaluated value using
$_
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.
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:
- Click over the result with right button
- Save as global variable
- copy(temp1)
Solution 3 - Javascript
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
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