footoss
footoss

Reputation: 63

How to change the value of js local variable when debug in Chrome console

When I debug javascript with the Chrome console, I want to change a local variable of a function. I know how to change the value of global variables, but how do I change the value of a local variable when debugging in the Chrome console?

Upvotes: 3

Views: 7866

Answers (2)

pshirishreddy
pshirishreddy

Reputation: 746

Try setting a break point where you would want to redefine the local variable's value to a different one. Then you get access to all the data in that scope.

This might help

enter image description here

Upvotes: 3

T.J. Crowder
T.J. Crowder

Reputation: 1074138

You don't debug in the Chrome console. You do debug in the Chrome debugger. And if you are stopped at a breakpoint in the debugger, you can use the console to change the value of any in-scope variable by assigning to it.

For instance, open dev tools and run this code, reading the comments:

function foo() {
  var bar = 42;
  // Normally, you don't have to use a hardcoded breakpoint like
  // the one that follows, you can set a breakpoint from within the
  // debugger just by navigating to the line of code and clicking in
  // the left-hand gutter. But in Stack Snippets the easiest way to
  // do one is to use the debugger statement:
  debugger;
  // Now, when stopped on the breakpoint, type this in the console:
  // bar = 67;
  // ...and press Enter.
  // Then hit the arrow button to allow the script to continue
  console.log(bar); // ...and this will log 67 instead of 42.
}
foo();

Upvotes: 4

Related Questions