Four Ways to Display Data
JavaScript Output
How to see what your JavaScript is doing — using console.log, innerHTML, alert, and document.write.
What you'll learn
- Print to the developer console with `console.log()`
- Update the page with `innerHTML` and `textContent`
- Know when (and when not) to use `alert()` and `document.write()`
While you’re learning JavaScript, you need ways to see what your code is doing. JavaScript gives you four common ways to show output.
1. console.log() — For Developers
The most common way to see what your code is doing. It writes a message
to the browser’s developer console (open it with F12 or
Ctrl/Cmd + Shift + I).
console.log("Hello, console!");
console.log(2 + 2);
console.log({ name: "Ada", year: 1815 }); console.log() accepts anything — strings, numbers, objects, arrays.
It’s the workhorse of JavaScript debugging.
2. innerHTML and textContent — Write to an Element
Most of the time, you actually want to display something on the page, not just in the console. Grab an element and set its content.
<p id="greeting"></p>
<script>
document.getElementById("greeting").textContent = "Hello, page!";
</script> - Use
textContentwhen you want plain text. - Use
innerHTMLwhen the value contains HTML (e.g."<strong>bold</strong>").
3. alert() — Pop Up a Message
Shows a modal popup. Useful for quick experiments, almost never for production.
alert("Hello! I'm an alert."); 4. document.write() — For Quick Tests Only
Writes directly to the document.
<script>
document.write("Hello from document.write!");
</script> Which One Should You Use?
| Method | Use for |
|---|---|
console.log() | Debugging — your daily companion |
innerHTML / textContent | Showing things on the page |
alert() | Almost never — annoying UX |
document.write() | Almost never — destructive |
For the rest of this track, you’ll see console.log() in most examples
and the DOM methods (textContent, innerHTML) when we’re working
with a real page.
Up Next
Now that you can run code and see its output, let’s talk about how JavaScript code is structured.
JavaScript Statements →