Lesson goal: Use console messages to identify and correct simple mistakes.
Errors are a normal part of programming. Your first task is to read the message carefully and find the line it describes.
Three useful console methods
console.log("The script is running.");
console.warn("Practice warning: remember to save your work.");
console.error("Practice error message: this is only a demonstration.");
| Method | Purpose |
|---|---|
console.log() |
Displays general information |
console.warn() |
Displays a warning-style message |
console.error() |
Displays an error-style message |
Calling console.error() does not itself stop the program or throw an error. It displays a message using the Console’s error presentation.
Exercise: Create and fix a spelling error
Temporarily replace the contents of script.js with:
Console.log("Welcome to JavaScript!");
You will usually see a message similar to:
Uncaught ReferenceError: Console is not defined
The correct name is console, with a lowercase c.
Fix the line:
console.log("Welcome to JavaScript!");
Expected output:
Welcome to JavaScript!
Try this incorrect code:
console.log("Welcome to JavaScript!);
The browser reports a SyntaxError. The exact wording can differ between browsers.
Correct code:
console.log("Welcome to JavaScript!");
Read the filename and line number
An error may include a link such as:
script.js:1
script.js.Click the link to inspect the code. Sometimes the actual mistake is just before the position reported, so check nearby characters too.
What if nothing appears?
| Possible cause | What to check |
|---|---|
| File was not saved | Save script.js, then refresh |
| Wrong filename | Match script.js exactly in HTML and on disk |
| File is in another folder | Correct the path in src |
| Wrong browser tab | Open the Console for your student profile page |
| Messages are hidden | Clear Console filters and show information-level messages |
| Syntax error | Read the first relevant error and correct the code |
| Old error remains visible | Clear the Console and refresh to check the current result |
A simple debugging routine
Your turn
Fix these two lines separately:
console.Log("I can find errors.");
console.log("I can fix errors.";
console.log("I can find errors.");
console.log("I can fix errors.");
The Console helps you find mistakes. Start with the error message, make one correction, and run the code again.