-
Notifications
You must be signed in to change notification settings - Fork 2
Debugging JavaScript with Browser DevTools
As a developer you will often want to debug code. You might have already used console.log
in some of the challenges, which is the simplest way to debug.
In this article we will tell you some of the coolest tricks, to debug using the native debug-tools of the browsers.
Before jumping into debugging lets leak out some secret facts about that awesome code checking engine at FCC.
We use a customized CodeMirror, as the code editor. A eval()
function is used to evaluate the JavaScript code represented as a string from the editor. When eval()
is called, browsers will natively execute your code. We will learn more why this secret is important in later sections of this article.
Google Chrome is one of the most popular browsers with a built-in JavaScript engine called V8, and offers a great toolset for developers called Chrome DevTools. Visiting their Complete JavaScript debugging guide is highly recommended.
Hit F12
. Alternatively you can press
Ctrl
+ Shift
+ I
on Windows and Linux or
Cmd
+ Shift
+ I
on Mac, or If you just love your mouse go to Menu > More Tools > Developer Tools
.
These two tabs are the perhaps going to be your best friends while debugging. The Sources
tab can be used to visualize all the scripts that's on the webpage you are visiting. This tab has sections for the code window, file tree, call stack & watch windows, etc.
The console
tab is where all of the log output goes. Additionally you can use the console tab's prompt to execute JavaScript code. Its kind of synonymous to the command prompt on Windows, or terminal on Linux.
Tip : Toggle the console anytime in the DevTools using Esc
key.
While you can visit the complete list of shortcuts, below are a few that are most used:
Feature | Windows, Linux | Mac |
Search for a keyword, regex is supported. | `Ctrl`+`F` | `Cmd`+`F` |
Search and Open a file | `Ctrl`+`P` | `Cmd`+`P` |
Jump to line | `Ctrl`+`G`+`:line_no` | `Cmd`+`G`+`:line_no` |
Add a breakpoint | `Ctrl`+`B`, or click on the line no. | `Cmd`+`B`, or click on the line no. |
Pause / resume script execution | `F8` | `F8` |
Step over next function call | `F10` | `F10` |
Step into next function call | `F11` | `F11` |
One of the coolest feature that you will love is debugging Dynamic Script, on the fly, via Source Maps.
Every script can be visualized in the Source tab of the DevTools. The source tab has all the JavaScript source files. But the code from the code editor is executed via eval()
in a container simply called a virtual machine(VM) within the browser process.
As you might have guessed by now is that our code is actually a script that doesn't have a file name. So we don't see that in the Source tab.
✨ Here comes the hack! ✨
We must leverage Source Maps
to assign a name to the JavaScript from our editor. Its pretty simple:
Lets say we are on the Factorialize challenge, and our code looks like this:
function factorialize(num) {
if(num <= 1){
...
}
factorialize(5);
All we need to do is add //# sourceURL=factorialize.js
to the top of the code, i.e the first line:
//# sourceURL=factorialize.js
function factorialize(num) {
if(num <= 1){
...
✨ And Eureka that's it! ✨
Now open up the DevTools and search for the file name. Add break points, Debug away and enjoy!
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links