On Github juliepagano / javascript-debugging-for-beginners-presentation
The best way to learn JavaScript debugging is to try it.
These slides are interactive and built in the browser!
Follow along: bit.ly/js-debug-present
The JS in this presentation is not necessarily good code.
Today we're focusing on debugging, not on code quality.
My slides have a bunch of silly Dune references because my original post does. I regret nothing.
The alert displays a dialog with the specified string value and an “ok” button to dismiss it.
alert("Hello! I am an alert.");
It will stop the JS from continuing until you click “ok”.
This is useful for debugging because you can set the alert value to something meaningful.
// I want to know if I reach this part of the code. alert("I am here!"); // I want to know the value of foo in this part of the code. alert("Foo: " + foo);
If you accidentally put an alert in a place that runs a lot of times, it can get annoying really fast.
// I heard you like alerts... for (i = 0; i < 100; i++) { alert(i); }
In the past, I occasionally had to kill a browser and restart it.
Thankfully most modern browsers have given us an out.
Way back in the day, alerts were one of the main tools people used to debug their JavaScript.
Unfortunately, the alert is a very limited tool. It can only display strings.
// I want to see all the H2s on the page. alert($('h2'));
Well, that's pretty useless.
Why am I mentioning alerts if they’re old and limited?
Because sometimes you still need to fall back on them.
Thankfully, we rarely need them now.
I use the chrome developer tools.
My examples use the chrome developer tools.
Most other modern browsers have similar tools.
Accessing the developer tools.
In many programming languages, you do debugging and logging in the terminal. The console is the equivalent for JavaScript in the browser.
The console tab shows JavaScript errors.
The console has an interactive command line that can be useful for debugging.
// Try some math. 2 + 2
// How about some string manipulation "the golden " + "path"
// You can even create an alert alert("Muad'Dib!");
// You can create variables. var arr = [1, 2, 3];
// You can add multiple lines by pressing shift + enter. for (var i = 0; i < arr.length; i++) { arr[i] = arr[i] * 2; } arr;
The console provides a bunch of helper functions. See the command line api reference for more information.
// You can look up elements via css selectors $$('h2');
// Or xpath $x('//h2');
You can access libraries defined on the page. For example, this page has jQuery.
// You can make the page red. $('body').css('background-color', 'red');
// That's a bit much. Let's put it back. $('body').css('background-color', '');
You can access variables in the current context.
// The current context. this;
// Access a variable I set in this context. exampleVariable.join(' ');
The console.log method outputs a message in the console.
console.log("I am logging to the console.");
The JS equivalent of printing to terminal to debug.
Just like an alert, we can use it to output values or check that we reach certain places in the code.
// I want to know if I reach this part of the code. console.log("I am here!");
// I want to know the value of foo in this part of the code. console.log("Foo: " + foo);
Unlike an alert, console.log does not stop the code.
// This isn't nearly so annoying with console.log. for (i = 0; i < 100; i++) { console.log(i); }
Alerts could only output strings.
Console.log can output any JS object.
// It can output dom elements. console.log($('h2'));
// It can output objects. console.log({ book: "Dune", characters: ["Paul", "Leto", "Jessica", "Chani", "sandworms"] });
This is just the beginning. There is a lot more you can do.
See the console api reference for more.
Console.log is great! Many people stop there and get by.
However, it's not always the best tool for the job.
Debugging complex JS using console.log can be tedious.
Thankfully, there are more tools!
The developer tools have an interactive JavaScript debugger that provides a rich toolset useful for debugging your code. I find this especially useful for complex JavaScript code and code that interacts with other libraries.
You can start the debugger at a specific point in your code by calling debugger.
// I want to start debugging here. debugger;
The debugger will only do something when the developer tools are open.
I’m not going to go into much more detail about using interactive debugger. The debugging javascript documentation already does a great job of this, so it seems silly for me to duplicate it.
Sometimes, debugging needs to dig into interactions with other libraries you depend on.
In production, minified JavaScript code is preferred. Most libraries will provide a minified version.
Minified code is hard to read and debug.
When possible, it is helpful to switch to the original code during development. Most libraries will provide unminified code.
Some libraries now provide source maps.
You can find minified jQuery in the sources tab. /bower_components/jquery/dist/jquery.min.js
You can use pretty-print to improve readability.
The network tab of the developer tools is really helpful for testing issues related to ajax requests.
Let's test it out looking at horse_js on twitter.
Another topic for another day, but here are some links.
You can emulate some mobile interactions using the developer tools, so that you can do debugging on your desktop browser.
Check out the mobile emulation documentation for more information.
Debugging with real (or simulated) mobile devices.