๐ Debugging Errors
Code not working? Follow the steps in the debugging process below before asking a peer or your teacher! Fixing your own errors, no matter how small, is one of the best ways to become a better coder.
๐งฑ HTML Debugging Process
-
Check for Proper Tag Structure
- Every opening tag needs a closing tag:
<div> ... </div>
- Self-closing tags:
<img />
,<br />
,<input />
- Every opening tag needs a closing tag:
-
Check for Typos
- Misspelled tags wonโt work:
<boddy>
โ<body>
- Misspelled attributes:
herf
โhref
,clas
โclass
- Misspelled tags wonโt work:
-
Check Nesting
- Tags must be properly nested: โ
<p><strong>text</strong></p>
โ<p><strong>text</p></strong>
- Tags must be properly nested: โ
-
Check File Paths
- Are images or stylesheets loading? Check:
src="images/cat.jpg"
orhref="style.css"
- Are images or stylesheets loading? Check:
-
Use the Validator
- https://validator.w3.org to check for structural errors
๐จ CSS Debugging Process
-
Check Selectors
-
Make sure the selector matches the element:
- ID:
#main-header
โ<h1 id="main-header">
- Class:
.card
โ<div class="card">
- ID:
-
-
Check Property Spelling and Semicolons
- โ
color: red;
โcolr red
or missing;
- โ
-
Check File Links
- Did you link the stylesheet?
<link rel="stylesheet" href="style.css">
- Did you link the stylesheet?
-
Check Order and Specificity
- Inline styles override external ones
- More specific selectors override general ones
๐ช JavaScript Debugging Process
-
Check the Console for Errors
-
Read the first error and look for:
Uncaught ReferenceError
โ variable not definedUncaught TypeError
โ trying something on the wrong type
-
-
Check Capitalization and Spelling
- JS is case-sensitive:
document
โDocument
- Watch for typos like
getElemntById
- JS is case-sensitive:
-
Check Syntax
- Missing semicolons (
;
) - Matching
()
and{}
and[]
- Strings need quotes:
"text"
or'text'
- Missing semicolons (
-
Check If the Script Is Linked Properly
-
In your HTML:
- โ
<script src="script.js"></script>
- Put it at the bottom of
<body>
, or usedefer
- โ
-
-
Use
console.log()
Often- Print variable values to the console to check whatโs happening
- Example:
console.log(myVar);
-
Check DOM Selectors
- Does the element exist in the HTML?
- Use
document.querySelector("#id")
or.class
- If youโre getting
null
, check spelling and that the element is loaded