Before you can fix code, you need to see what it's doing. JavaScript can show output in three places: the console, which is for you as the developer; pop-up boxes; and the page itself, which is for your visitors.
The console is the most important of these while you're learning. Professional developers use it all day to check values, follow the order code runs in and read error messages. Getting comfortable with it now will make every later lesson easier.
The console methods
| Method | Use |
|---|---|
console.log() | General messages and values |
console.info() | Informational messages |
console.warn() | Warnings, shown in yellow |
console.error() | Errors, shown in red |
console.table() | Arrays and objects shown as a table (in real DevTools) |
console.clear() | Clears the console |
You can pass several values separated by commas: console.log("Score:", score, "Lives:", lives). The console prints them on one line with spaces between them.
Showing output to visitors
element.textContent = "…"changes text on the page. It's the best way to show results to the people using your site.alert("…")shows a pop-up that blocks the page until it's closed. It's fine for quick tests but annoying in a real site. (On this site, alerts are shown in the console instead.)document.write()is an old method that can wipe the whole page if it runs at the wrong moment. Don't use it.
Visitors never see the console, so anything they need to know must be written into the page.
Using console.log to find bugs
When code doesn't do what you expect, add a console.log() just before the line you suspect and print the values involved. You'll often find that a variable holds something different from what you assumed, such as text instead of a number, or undefined because of a spelling mistake.
Remove or comment out your debugging logs when you've finished. Leaving dozens of them in place makes real problems harder to spot later.
JavaScript Output Syntax
Output methods
console.log("value:", x);
console.warn("careful");
console.error("broken");
document.querySelector("#out").textContent = "Done!";JavaScript Output Summary
- The console is where developers see values, messages and errors.
- Use
console.log,console.warn,console.errorandconsole.tablefor different kinds of output. - Show results to visitors by changing
textContenton the page. - Avoid
alert()in real sites and never usedocument.write(). - Label your logs and print values to track down bugs.
JavaScript Output Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Different kinds of console messages
console.log("Game started");
console.info("Level 1 loaded");
console.warn("Low health: 10%");
console.error("Connection lost!");
console.log("Score:", 1200, "Lives:", 3);Line-by-line explanation
- Line 1A plain message.
- Line 2Info messages get a blue "i" here.
- Line 3Warnings are yellow: something might be wrong.
- Line 4Errors are red: something is wrong.
- Line 5Several values in one log, separated by spaces in the output.
Logging different types
The console shows strings, numbers, arrays and objects differently.
console.log("text");
console.log(42, true, null);
console.log(["apples", "pears"]);
console.log({ name: "Biscuit", age: 3 });
console.table([{ fruit: "kiwi", qty: 4 }, { fruit: "mango", qty: 2 }]);Line-by-line explanation
- Line 1Strings print without quotes at the top level.
- Line 2Numbers, booleans and
nullare highlighted. - Line 3Arrays show their items in square brackets.
- Line 4Objects show key: value pairs.
- Line 5In real browser DevTools this shows a neat table.
Showing output on the page
<p>Temperature: <strong id="temp">…</strong></p>const celsius = 21;
const fahrenheit = celsius * 9 / 5 + 32;
document.querySelector("#temp").textContent = fahrenheit + "°F";
console.log("Converted", celsius, "→", fahrenheit);Line-by-line explanation
- Lines 1 to 2Convert Celsius to Fahrenheit.
- Line 4Users see the result on the page.
- Line 5You see a debug message in the console.
Common JavaScript Output Mistakes
Forgetting to log
5 + 5;
"hello".toUpperCase();console.log(5 + 5);
console.log("hello".toUpperCase());In a script, results aren't printed automatically. Wrap them in console.log() to see them.
Joining text and numbers by accident
console.log("Total: " + 2 + 3); // Total: 23console.log("Total: " + (2 + 3)); // Total: 5
console.log("Total:", 2 + 3); // Total: 5Once a string is involved, + glues things together. Use brackets or commas.
JavaScript Output in Real Projects
Logging in production
Real apps use the console during development, then remove noisy logs before launch. In production, errors are often sent to monitoring tools (like Sentry) that alert developers when something breaks for users.
JavaScript Output Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Print a warning and an error.
console.("Battery low");
console.("Battery empty!");
console.warn for warnings, console.error for errors.What does this print?
console.log("Total: " + 4 + 6);What is the best way to show a result to users on the page?
This should print Level: 5 and show Level 5 in the paragraph. Fix it.
<p id="level"></p>const level = 5;
console.log("Level: " level);
document.querySelector("#level").textcontent = "Level " + level;- Logs "Level: 5"
- The paragraph says "Level 5"
JavaScript Output Quiz
3 questions to check what you learned.
Which method prints a red error message?
- console.error
- console.log
- console.red
- console.fail
What does
console.log("a", "b")print?- ab
- "a", "b"
- a b
- a,b
Why should you avoid
document.write()?- It's too slow
- It can overwrite the whole page after it has loaded
- It only works in Firefox
- It prints to the console
JavaScript Output Challenge
Receipt printer
Using the variables given:
- Log
Item: Coffee - Log
Price: 3.5 - Write
Coffee costs £3.5into the#receiptparagraph usingtextContent
<p id="receipt"></p>const item = "Coffee";
const price = 3.5;
- Logs "Item: Coffee"
- Logs "Price: 3.5"
- Receipt shows "Coffee costs £3.5"