Making your code easy to troubleshoot
- It helps greatly if you use the tab key to indent your code so that blocks of code are easily identified. This makes your code much easier to read and allows you to skim through the code looking for the section you are interested in.
- Put comments in your code to help you know what something is doing.
- In HTML, comments look this this: <!– This is a comment — >
- In JavaScript, comments look like this: //This is a comment
- Use variable names that clearly tell you what the variable represents.
- This is a bad variable name: var1
- This is a good variable name: questionText
Common errors
Be on the lookout for common errors.
In HTML, these include:
- Forgetting to close a tag with </a>, </p>, </div>, etc.
- Forgetting to put in a <br> to go to the next line.
In JavaScript, these include:
- Forgetting to finish a line with a semicolon
- Forgetting to close out a block with a }
Use the console
You can access the console on the web browser by pressing Ctrl + Shift + i.
The console may display an error message. If so, it may give you good information about what went wrong. Sometimes the error message is not particularly useful, but it will give you the line number of the code where the error occurred.
You can type a variable name on the console to see its current value. Sometimes this will help you understand what went wrong, especially if it is not the value you were expecting.
You can also have the JavaScript print to the console to show what the value of a variable is while the code is running. The syntax for this is:
console.log(t);
where t is the variable you want to display.
Leave a comment