JavaScript Output

Four Ways to Display Data

JavaScript Output

How to see what your JavaScript is doing — using console.log, innerHTML, alert, and document.write.

6 min read Level 1/5 #output#console#innerHTML
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).

Logging to the console script.js
console.log("Hello, console!");
console.log(2 + 2);
console.log({ name: "Ada", year: 1815 });
▶ Preview: console

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.

Writing to an element index.html
<p id="greeting"></p>
<script>
  document.getElementById("greeting").textContent = "Hello, page!";
</script>
▶ Preview: iframe
  • Use textContent when you want plain text.
  • Use innerHTML when 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.

Showing an alert script.js
alert("Hello! I'm an alert.");

4. document.write() — For Quick Tests Only

Writes directly to the document.

document.write index.html
<script>
  document.write("Hello from document.write!");
</script>
▶ Preview: iframe

Which One Should You Use?

MethodUse for
console.log()Debugging — your daily companion
innerHTML / textContentShowing 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 →
Reference