Programing

Access last logged value in Chrome console

lottogame 2020. 12. 2. 07:44
반응형

Access last logged value in Chrome console


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?


After it's been logged to the console, you can right click on it and get an option to Store as global function. 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).


Just follow these steps:

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

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


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.


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


I think $0 is what you're looking for.

참고URL : https://stackoverflow.com/questions/15895579/access-last-logged-value-in-chrome-console

반응형