1

How do I set a breakpoint in the global scope on Chrome Dev Tools? I have my variables in the local function scope below. How would this look in the global scope? Thanks!

function calc() {
  var a = parseInt(document.querySelector('#value1').value);
  var b = parseInt(document.querySelector('#value2').value);
  var op = document.querySelector('#operator').value;
  var calculate;

  if (op == 'add') {
    calculate = a + b;
  } else if (op == 'min') {
    calculate = a - b;
  } else if (op == 'div') {
    calculate = a / b;
  } else if (op == 'mul') {
    calculate = a * b;
  }
  document.querySelector('#result').innerHTML = calculate;
}
  • What do you mean by "set a breakpoint in the global scope"? A breakpoint triggered when the code on that specific line is run, irregardless of scope. Please clarify the question. – Papooch May 21 '21 at 07:28
  • 1
    Thank you, I was wondering - what's the difference between setting a breakpoint between the global and function scope? – Chaz Carothers May 22 '21 at 23:26
  • 1
    If you set a breakpoint on a code that is inside a funciton, it triggers when the line in that function is about to be evaluated; if you set a breakpoint in an outer scope on the line where that function is used, it triggers when the function is about to be called. The global scope is the html document, if there's any javascript, you can as well set breakpoints on that. That's really all there is to breakpoints and "scope". Is that what you were asking? I'm still not sure if I get where you're comming from... – Papooch May 24 '21 at 07:11
  • I guess the only other thing that could be called "global breakpoint" would be a global conditional breakpoint, which is a breakpoint that is triggered as soon as a certain condition is met ANY TIME, i.e. it is not specific to a specific code block or line. Borland´s Turbo Debugger was able to do such magic. Not sure if the Chrome debugger can do that. I think: No – TheBlastOne Dec 08 '22 at 13:36
  • Oh, and let me add, while there might be some CPU support to implement such magic, the code would be executed quite slowly, as after every statement, the condition would need to be evaluated (in order to detect if the condition is met and the breakpoint should trigger). On an old x86 platform, I´ve been waiting for global conditional breakpoints to trigger for MINUTES while in uninstrumented "realtime", it would take less than a second to execute the same code. But for nasty bugs in alien code, this wait is worth the findings you can get this way. – TheBlastOne Dec 08 '22 at 13:41

0 Answers0