99

I'm debugging a javascript app (using Chrome dev tools), and I would like to change some variable values while stepping through the code.

Is that possible at all?

I have tried and got something like:

> modeline
1
> modeline=0
0             <<< seems to work but... 
> modeline
1             <<< ups!!

But I'm unable to find any documentation that states what can or can't be done...

GreenGiant
  • 4,930
  • 1
  • 46
  • 76
tato
  • 5,439
  • 4
  • 31
  • 27
  • Post some code. Where does `modeline` comes from? – Emil Ivanov Jan 05 '11 at 10:28
  • 5
    @Emil: Is that important? modeline is a global variable, I also have tried modifying it using window.modeline with same results. But this question is also relevant to local variables declared inside a function – tato Jan 05 '11 at 10:37
  • I do this frequently without issue. Could some outside function be modifying your variable? Try setting a breakpoint everywhere in the code where it is changed. – gilly3 Jan 31 '11 at 19:21
  • 1
    Filed a [bug report](http://code.google.com/p/chromium/issues/detail?id=163207) for this. – GreenGiant Nov 28 '12 at 22:14
  • 4
    I can confirm this behaviour. Modifying a property on a JS object in Chrome does not seem to have any effect on the actual value of the object in the interpreter. In Firefox the same modification makes the js script evaluate differently as you would expect. Some kind of exstra security in Chrome peraps? Does anyone know if it can be turned of in Chrome, so you can use it for debugging js? – Nikolaj Hansen Nov 11 '12 at 10:46
  • 1
    Already implemented in V8: [Issue 2399](http://code.google.com/p/v8/issues/detail?id=2399) Now Chromium's Developer Tools need to be updated: [Issue 124206](http://code.google.com/p/chromium/issues/detail?id=124206) – gabrielmaldi Mar 21 '13 at 17:55
  • 2016/01/19 Chrome latest, trivial code (`var a = 1; debugger; console.log(a);`) this bug is still present... – Offirmo Jan 19 '16 at 13:02
  • Still seeing this problem in Version 49.0.2623.110 m – bnieland Apr 04 '16 at 14:52

12 Answers12

72

Why is this answer still getting upvotes?

There is a better answer than this one after all these years, and I approve it since I'm using it all the time. Go upvote Tyler Collier's answer instead. They explain that you can either modify values in the console or in the stack trace. No need for a trick.


Obsolete answer

This is now possible in chrome 35 (today as of July 11, 2014). I don't know which version allowed it first though.

Just tested @gilly3 example on my machine and it works.

  • Open the console, in Sources and the tab Snippets, add a new snippet, paste the following code into it:

    var g_n = 0; function go() { var n = 0; var o = { n: 0 }; return g_n + n + o.n; // breakpoint here }

  • Right-click the snippet name, click 'Run' (this does not fire the function though)

  • Add the breakpoint at the return statement.

  • In the console below, type go()

  • and change the variable values as demonstrated below

function with local modification allowed.

and the returned result g_n + n + o.n is 30.

Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
  • 1
    The regression of this bug is tracked in [the link](http://code.google.com/p/chromium/issues/detail?id=124206) provided in [Brian's answer](http://stackoverflow.com/a/15842124/772086). – Mike Mar 29 '16 at 17:35
  • Chrome won't let me set a breakpoint in the snippet. – David Klempfner Jul 10 '21 at 07:24
40

Why is this answer still getting upvotes?

Per Mikaël Mayer's answer, this is no longer a problem, and my answer is obsolete (go() now returns 30 after mucking with the console). This was fixed in July 2013, according to the bug report linked above in gabrielmaldi's comment. It alarms me that I'm still getting upvotes - makes me think the upvoter doesn't understand either the question or my answer.

I'll leave my original answer here for historical reasons, but go upvote Mikaël's answer instead.


The trick is that you can't change a local variable directly, but you can modify the properties of an object. You can also modify the value of a global variable:

var g_n = 0;
function go()
{
    var n = 0;
    var o = { n: 0 };
    return g_n + n + o.n;  // breakpoint here
}

console:

> g_n = 10
  10
> g_n
  10
> n = 10
  10
> n
  0
> o.n = 10
  10
> o.n
  10

Check the result of go() after setting the breakpoint and running those calls in the console, and you'll find that the result is 20, rather than 0 (but sadly, not 30).

Community
  • 1
  • 1
gilly3
  • 87,962
  • 25
  • 144
  • 176
  • 2016/01/19 Chrome latest, trivial code (var a = 1; debugger; console.log(a);) the bug is still present... – Offirmo Jan 19 '16 at 13:18
  • @Offirmo - `1` is logged. What is the bug? What did you expect to be logged? – gilly3 Jan 22 '16 at 00:38
  • with the debugger open, change the value of `a` to anything (ex. 42) in the "scope" panel then resume : 1 is logged – Offirmo Jan 22 '16 at 10:36
  • @Offirmo - not for me: [screenshot](http://i.stack.imgur.com/QEe5P.png). But, if I wrap it in a function, then I see what you describe. Interesting. But then, I can change the value by entering it in the console directly, (which I personally find easier than using the scope panel). [screenshot](http://i.stack.imgur.com/mr85r.png) I guess I never tried changing values in the scope panel. I always just execute expressions in the console that change the value, and that seems to work no matter what. – gilly3 Feb 05 '16 at 21:40
  • `Why is this answer still getting upvotes?`because trying to modify large minified javascript files that can’t be pretty printed doesn’t works ? – user2284570 May 28 '16 at 12:37
  • 6
    I upvoted because your answer meets all the criteria of ["a good answer"](http://stackoverflow.com/help/how-to-answer) *and* you mention another answer that provides newer, better info, which — coming from an accepted answer — gives it instant credibility. So, thanks. And enjoy the upvotes! – gfullam Dec 15 '16 at 16:59
  • [Tyler Collier's answer](https://stackoverflow.com/a/50577313/641833) is more up to date than Mikaël Mayer's. Maybe it is time for another edit? – Trisped Nov 19 '20 at 01:37
  • Agreed. I added the same header. – Mikaël Mayer Jul 12 '21 at 15:03
34

Yes! Finally! I just tried it with Chrome, Version 66.0.3359.170 (Official Build) (64-bit) on Mac.

You can change the values in the scopes as in the first picture, or with the console as in the second picture.

Chrome debugger change values

enter image description here

Tyler Collier
  • 11,489
  • 9
  • 73
  • 80
8

This is an acknowledged bug in the Chrome Dev Tools:

http://code.google.com/p/chromium/issues/detail?id=124206

brianpeiris
  • 10,735
  • 1
  • 31
  • 44
  • Looks like it was fixed Mar 21, 2013. I'm not sure how to tell which version of chrome it will be in. – David Apr 17 '13 at 20:12
  • @David They fixed it in Blink, we might have to wait for some time to get a Chrome build which uses Blink – Anshul May 03 '13 at 20:16
  • 3
    It's broken (again) in in Chrome version 49.0.2623.87. Sign into the above link and star the issue if you want to elevate its importance. – Mike Mar 29 '16 at 17:33
5

It looks like not.

Put a breakpoint, when it stops switch to the console, try to set the variable. It does not error when you assign it a different value, but if you read it after the assignment, it's unmodified. :-/

Vlad GURDIGA
  • 1,284
  • 2
  • 14
  • 18
  • Ok... at least you are seeing the same behaviour that I see. But... Is it possible? – tato Jan 05 '11 at 10:50
5

Firebug seems to allow you to do that.

Bobo
  • 8,777
  • 18
  • 66
  • 85
3

I'm able to modify a script variable value by assignment in the Console. Seems simplest.

Jeff Lowery
  • 2,492
  • 2
  • 32
  • 40
2

Actually there is a workaround. Copy the entire method, modify it's name, e.g. originalName() to originalName2() but modify the variable inside to take on whatever value you want, or pass it in as a parameter.

Then if you call this method directly from the console, it will have the same functionality but you will be able to modify the variable values.

If the method is called automatically then instead type into the console

originalName = null;
function originalName(original params..)
{
    alert("modified internals");
    add whatever original code you want
}
mrk
  • 4,999
  • 3
  • 27
  • 42
mdubez
  • 3,024
  • 1
  • 17
  • 10
1

The only way I could change variable values with success (in addition to doing it in console window) is modifying the script directly in the chrome editor under "Sources" Tab (this changes the behavior of your script until you refresh the page), but that changes will be lost when a browser refresh, so be careful. NOTE: This works also if you want to modify any line of code of your script but it doesn't work with typescript files coming from Angular projects.

LeonardoX
  • 1,113
  • 14
  • 31
1

I do this differently than any of the other answers if I'm dealing with code that gets hit a lot (React render cycles).

  1. Make a conditional breakpoint.
  2. In the breakpoint condition, set your value flags.showAccountCard = false; -- the value will be set every time that breakpoint is hit.
  3. However if you use flags.showAccountCard = true; the breakpoint will be hit every time because the engine is returning the value that it was set to (true) and that value means the breakpoint should be hit. To get around this add false on the same line: flags.showAccountCard = true; false;. This will set the value to true but not actually stop on the breakpoint.

This will allow you to set a value every time the exec pointer hits that line without stopping execution.

Tested in Chrome 99.

jcollum
  • 43,623
  • 55
  • 191
  • 321
0

I was having the same issue, went to the 'About Google Chrome'->Help and it said I needed to restart my browser to get the latest updates.

I did this, and suddenly, I can now change local variables. Simply click the variable you want to edit in the Scope Variables window, and type in your new value.

I did notice some oddities though, that I had to step over some unrelated var assignments before I could alter the text in the right hand window (Scope Variables).

GilesDMiddleton
  • 2,279
  • 22
  • 31
  • If you've used local storage to store properties, you can also go to Resources->LocalStorage and edit the properties you have saved directly there. – GilesDMiddleton May 22 '13 at 11:20
0

To modify a value every time a block of code runs without having to break execution flow:

The "Logpoints" feature in the debugger is designed to let you log arbitrary values to the console without breaking. It evaluates code inside the flow of execution, which means you can actually use it to change values on the fly without stopping.

Right-click a line number and choose "Logpoint," then enter the assignment expression. It looks something like this:

enter image description here

I find it super useful for setting values to a state not otherwise easy to reproduce, without having to rebuild my project with debug lines in it. REMEMBER to delete the breakpoint when you're done!

Matthew Marichiba
  • 1,942
  • 1
  • 23
  • 24