Getting Started Lesson 3 of 30
JavaScript Output — console.log, textContent, innerHTML and alert
Four ways JavaScript shows results — writing into an element with textContent or innerHTML, printing to the console, alert boxes, and document.write.
A program that computes something is useless if nobody sees the answer. JavaScript has several ways to show output. You'll use two of them almost every day: changing an element's text, and printing to the console.
1. Writing into an element: textContent
Find an element, then set its textContent. This is the most common way to show results on a page.
<p>Your total: ₱<span id="total"></span></p>
<script>
const price = 15;
const qty = 4;
document.getElementById("total").textContent = price * qty;
</script>
2. innerHTML: writing HTML
innerHTML is like textContent, but the text is read as HTML, so tags work:
<div id="box"></div>
<script>
document.getElementById("box").innerHTML = "<h3>Honor Roll</h3><p><strong>Maria Clara</strong> – 96</p>";
</script>
3. The console: console.log()
console.log() prints a message to the browser's developer console. Visitors never see it, but you do. It's the number one tool for checking what your code is doing.
console.log("Hello, console!");
console.log(10 + 5);
console.log("Price:", 25, "Qty:", 3);
Console
Hello, console! 15 Price: 25 Qty: 3
To open the console in Chrome or Edge, press F12 (or Ctrl + Shift + J; on a Mac, ⌘ + ⌥ + J) and click the Console tab. On a phone there's no console, so in our Try-it editor you can show results on the page instead.
This little helper prints console-style lines on the page, so you can follow along even on a phone:
<pre id="log" style="background:#111;color:#8fd3b5;padding:10px;border-radius:8px;"></pre>
<script>
function show(...items) {
document.getElementById("log").textContent += items.join(" ") + "\n";
}
show("Hello, console!");
show(10 + 5);
show("Price:", 25, "Qty:", 3);
</script>
4. alert(): a pop-up box
<button onclick="alert('Bawal ang cellphone sa exam! 📵')">Read the rule</button>
alert() stops everything until the user clicks OK. It's handy for quick tests, but annoying on real websites, so use it rarely.
document.write(): avoid it
You might see document.write() in old tutorials. It writes straight into the page while it loads, and if it runs after the page has loaded it erases the entire page. Modern code uses textContent or innerHTML instead.
Which one should I use?
| Method | Who sees it | Use it for |
|---|---|---|
textContent | Visitors | Showing results and messages (safe) |
innerHTML | Visitors | Building HTML from your own code |
console.log() | Only you (developer) | Testing and debugging |
alert() | Visitors | Quick tests, rare warnings |
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04