Code is prone to errors. You are quite likely to make errors... Oh, what am I talking about? You are absolutely going to make errors, at least if you're a human, not a robot.
But in the browser, a user doesn't see the errors by default. So, if something goes wrong in the script, we won't see what's broken and can't fix it.
To see errors and get a lot of other useful information about scripts, "developer tools" have been embedded in browsers.
Most often developers lean towards Chrome or Firefox for development because those browsers have the best developer tools. Other browsers also provide developer tools, sometimes with special features, but are usually playing "catch-up" to Chrome or Firefox. So most people have a "favorite" browser and switch to others if a problem is browser-specific.
Look at the code below.
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> </head> <body> There is an error in the script on this page. <script> lalala </script> </body> </html>
key:F12 or, if you're on Mac, then
The developer tools will open on the Console tab by default.
It looks somewhat like this:
The exact look of developer tools depends on your version of Chrome. It changes from time to time but should be similar.
- Here we can see the red-colored error message. In this case, the script contains an unknown "lalala" command.
On the right, there is a clickable link to the source
bug.html:12with the line number where the error has occurred.
Below the error message, there is a blue
key:Enter to run them (
key:Shift+Enter to input multi-line commands).
Now we can see errors, and that's enough for a start. We'll be back to developer tools later and cover debugging more in-depth in the chapter info:debugging-chrome.
Firefox, Edge, and others
Most other browsers use
key:F12 to open developer tools.
The look & feel of them is quite similar. Once you know how to use one of those tools (you can start with Chrome), you can easily switch to another.
Safari (Mac browser, not supported by Windows/Linux) is a little bit special here. We need to enable the "Develop menu" first.
Open Preferences and go to "Advanced" pane. There's a checkbox at the bottom:
key:Cmd+Opt+C can toggle the console. Also, note that the new top menu item named "Develop" has appeared. It has many commands and options.
- Developer tools allow us to see errors, run commands, examine variables and much more.
They can be opened with
key:F12for most browsers under Windows. Chrome for Mac needs
key:Cmd+Opt+C(need to enable first).