Reputation: 121
How do I use Chrome dev tools to evaluate code within the local scope of a function? I.e. for the following code
const name1 ="alireza"
function person(){
const name2 ="joe"
}
the result of code manually executed in the console should be
console.log(name2) //"joe"
Upvotes: 1
Views: 2399
Reputation: 8988
What you are looking for are breakpoints:
You can dynamically set a breakpoint on a line within the function you want to inspect: devTools will break before the execution of that line and you can evaluate code within the scope of that function in the console.
Alternatively - if you can change the code (which you can also from devTools) - you can put a debugger
statement where you want the execution to break.
Upvotes: 3
Reputation: 943578
This is slightly tricky because the statement you want to examine is the only statement in the function.
If you were to add another statement on the following line:
const name1 ="alireza"
function person(){
const name2 ="joe"
const name3 ="bob";
}
Then you could:
const name2 ="joe"
person()
to run the person
functionconsole.log(name2);
(or you could just look at the debugging information in the Debugging tab which reports all the in-scope variables).Upvotes: 1