Chrome Developer Tools: Step vs Step Into Explained
In this article
Yes, “step” and “step into” in Chrome DevTools are distinct debugging actions; “step into” allows you to drill down into function calls, while “step” executes the current line without entering the function. This guide explains the differences and how to use them effectively.
What You’ll Need
- Google Chrome Browser
- An open web page with JavaScript code
- Basic understanding of JavaScript concepts (functions, variables)
Step-by-Step
- Open Chrome DevTools: Right-click on the webpage and select “Inspect” or use the keyboard shortcut Ctrl+Shift+I (Windows/Linux) or Cmd+Option+I (Mac).
- Navigate to the Sources Panel: In the DevTools panel, click on the “Sources” tab. This is where your JavaScript code is located.
- Set a Breakpoint: Click in the left-hand gutter (the area next to the line numbers) to set a breakpoint. A red marker will appear on the line of code where execution will pause. Breakpoints are crucial for controlling the debugging process. You can also right-click on a line of code and select “Add Breakpoint.”
- Run the Code: Make sure the page is loaded and the JavaScript code is executing. The debugger will automatically pause at your breakpoint.
- Step Over: With the debugger paused, use the “Step Over” button (it looks like a small arrow pointing over a line) to execute the current line of code. This will move to the next line without stepping into any function calls. This is useful when you want to skip over a function call you don't need to examine closely.
- Step Into: Now, use the “Step Into” button (it looks like a small arrow pointing into a line) to step *into* a function call. This will pause execution at the first line of code inside the called function. This allows you to examine the execution flow within the function. If the function call is not a function call, then “Step Into” will behave like “Step Over.”
- Step Out: If you're inside a function and want to return to the calling function, use the “Step Out” button (it looks like a small arrow pointing out of a line). This will execute the rest of the current function and return to the line where it was called.
- Step Continue: Use the “Step Continue” button (it looks like a forward-pointing arrow) to continue execution until the next breakpoint is hit, or the end of the script.
Common Mistakes
- Not Setting Breakpoints: Without breakpoints, the debugger won’t pause, and you won’t be able to step through the code.
- Incorrectly Using “Step Into”: If the function call is not a function call (e.g., it's a variable assignment), “Step Into” will treat it as “Step Over.”
- Ignoring Return Values: Pay attention to the values of variables and expressions as you step through the code to understand how they change.
Tips
- Use Conditional Breakpoints: Set breakpoints that only trigger when a specific condition is met. Right-click on a breakpoint and select “Edit Breakpoint.”
- Inspect Variables: Use the “Scope” panel in DevTools to examine the values of variables in the current scope. This is essential for understanding how your code is working.
- Simplify Your Code: If you're having trouble debugging, try simplifying your code to isolate the problem.
- Understand Call Stacks: The “Call Stack” panel shows the sequence of function calls that led to the current point of execution. This can be helpful for understanding the flow of your code.
Troubleshooting
- Debugger Not Pausing: Ensure that your breakpoint is actually on the line of code where you expect execution to pause. Sometimes breakpoints are accidentally placed on the wrong line.
- Debugger Pausing Unexpectedly: This could be due to a JavaScript error. Check the “Console” panel in DevTools for any error messages.
- “Step Into” Not Stepping Into: This usually means the function call isn't a function call. Try “Step Over” instead.
- No Breakpoints Showing: Verify that the “Sources” tab is open and that you've actually set a breakpoint.
- Chrome DevTools is not working: Try restarting Chrome. If that doesn't work, clear your browser cache and cookies.
Frequently Asked Questions
What does “Step Over” do?
“Step Over” executes the current line of code, including any function calls, without stepping into the function calls themselves. It moves to the next line of code in the current function.
When should I use “Step Into”?
Use “Step Into” when you want to examine the execution flow within a function call. This is helpful for understanding how functions are called and how they modify variables.
How do I remove a breakpoint?
Click on the red marker of the breakpoint in the left-hand gutter to remove it. Alternatively, right-click on the breakpoint and select “Remove Breakpoint.”
Can I set multiple breakpoints?
Yes, you can set as many breakpoints as you need. Each breakpoint will pause execution at its respective line of code.
What is the Call Stack?
The Call Stack is a panel in DevTools that shows the sequence of function calls that led to the current point of execution. It’s a visual representation of how your code is being executed, and can be very helpful for debugging complex code.