JavaScript (Reference)
What are the issues when debugging Javascript that runs in the web browser?
- The issue: Show errors/debug info without messing up the page.
- Debugging often involves writing out debug data to see what is going on.
Where do you write debug info to?
Don't want to mess up the page.
-
Code errors may also generate JS error messages, but do not want these on the page.
-
Q. Does the client want to see Javascript errors?
- The answer:
The JS console
- JS error messages are written to the console.
- Write debug info to console with
console.log
- Doing your own error-handling:
How to access console/debug in different browsers
- Chrome
- Chrome DevTools
- View source and inspect elements in yours or other people's pages.
- Chrome - Tools - Developer tools (or Ctrl-Shift-I, or "Inspect element")
- JavaScript Console
(Ctrl-Shift-J)
- Ctrl-Shift-F to search
- Can search for Javascript function name to view the source.
- Firefox
- Edge / IE
console.log filter
- On Chrome you can filter console.log messages by error/warning/informational.
- A subtle bug:
- Chrome stopped displaying console.log output from my program.
- I thought my program was at fault. Spent some time trying to debug program.
- Eventually I discovered the console.log filter level had somehow been changed,
so informational messages were hidden.
-
Return filter to default and my program output was visible again.
View object
- Just type name of object in console to see its structure.
- May need to type: console.dir(object)
- Printing out a complex object:
View page "source" after JS
"Beyond Console Log",
by the
Fireship channel.
The "console" object has many features to support debugging.
See all
console functions.