There are 2 aspects to debugging:

  1. Debugging Tools
  2. Mindset to tackle errors/bugs etc.

<aside> 💡 Here we will learn about Debugging Tools

</aside>

  1. Console Methods:

    1. Call Stack or Stack Trace: Tells us what function called what function called what function and so on..

    Example:

    function doctorize(name) {
      // console.count(`running Doctorize for ${name}`);
      return `Dr. ${name}`;
    }
    
    function greet(name) {
      doesntExist(); // Cause an error
      return `Hello ${name}`;
    }
    
    function go() {
      const name = doctorize(greet('Wes'));
      console.log(name);
    }
    

    In this above example, doesntExist function doesn't exist, which will cause an error, when we run the go function:

    go()

    Error:

    debugging.js:32 Uncaught ReferenceError: doesntExist is not defined
        at greet (debugging.js:32)
        at go (debugging.js:37)
        at <anonymous>:1:1
    

    The error says that this error occurred at line 32 in function greet. greet was called by go at line 37. The anonymous 1:1 comes as we ran it from our console, else it would have shown the line no. from where we would have called the go function in code.

    1. Grabbing Elements

    If we select something using the Elements tab in dev tools, and then flip over to Console tab, then we run $0, then we get returned that selected item.

    $0: the selected element

    $1 : the last selected element

    $2 : the second last selected element

    and so on...

    We also have $ and $$ in the console. We can't use them in code.

    They are shorthand selectors.

    1. Breakpoints:

    To pause JavaScript from running at a certain line of code, we write debugger;. The pausing of JavaScript only works when the DevTools is open. It helps us to peer into JavaScript at that very moment.

    It shows us the call stack, values of local variables at that time and other useful stuff.

    There are 2 buttons:

    play/pause: clicking this runs JS till the next time debugger; is encountered in code.

    step over next function call: runs the code line by line.

    We can also set breakpoints from the browser by ourselves in the sources tab. This does the same thing as a debugger; i.e. stops the JS from running at that point.

    1. Network Requests:

    To see the network requests, go to the Network tab in Devtools.

    1. Break on Attribute:

    We can select an element in Elements tab and right click on it to select break on > attribute modifications. It means, when somebody changes its attributes, then there will be a breakpoint.

    1. Other types of breakpoints:

    In Sources tab, there are other type of breakpoints too like mouse click, keyboard events, XHR or fetch breakpoints(breakpoint when a XHR request is made) etc.