TExcelTAD
Home › JavaScript › JS Output

JavaScript Output

JavaScript has no single "print" statement. Instead, it can show data in several places depending on where it runs: the developer console, the page itself, or a pop-up dialog.

Method Where the output goes Typical use
console.log() The developer console Debugging, learning, Node.js programs
element.textContent = … Into an HTML element Real user-facing output
element.innerHTML = … Into an element, parsed as HTML Inserting markup you control
alert() A modal dialog box Quick demos (annoying in real apps)
document.write() Directly into the page while it loads Legacy — avoid it

console.log()

console.log accepts any number of values and prints them separated by spaces. Strings are printed as-is, while numbers, booleans, arrays and objects are shown in a readable form.

js · try it yourself
console.log("Plain text");
console.log(10, 20.5, true, null, undefined);
console.log([1, 2, 3]);
console.log({ name: "Ada", born: 1815 });
console.log("Nested:", { tags: ["math", "code"], active: false });
Plain text
10 20.5 true null undefined
[ 1, 2, 3 ]
{ name: 'Ada', born: 1815 }
Nested: { tags: [ 'math', 'code' ], active: false }

Notice that strings inside arrays and objects are shown with single quotes — that tells you they are strings and not numbers.

Other console methods

The console has more tools than log:

js · try it yourself
console.info("Information");
console.warn("Careful: disk almost full");
console.error("Something failed");

console.table([
  { item: "Pen", price: 1.5 },
  { item: "Notebook", price: 3 },
]);

console.group("Order #12");
console.log("2 items");
console.log("Total: 4.50");
console.groupEnd();

console.count("clicks");
console.count("clicks");
Information
Careful: disk almost full
Something failed
┌─────────┬────────────┬───────┐
│ (index) │ item       │ price │
├─────────┼────────────┼───────┤
│ 0       │ 'Pen'      │ 1.5   │
│ 1       │ 'Notebook' │ 3     │
└─────────┴────────────┴───────┘
Order #12
  2 items
  Total: 4.50
clicks: 1
clicks: 2

In a browser's developer tools, warnings and errors are highlighted in yellow and red, and console.table draws a sortable table.

Writing into the page

Users never see the console. To show something to them, put it into an HTML element. textContent sets plain text; innerHTML parses the string as HTML:

html · try it yourself
<p id="plain"></p>
<p id="rich"></p>
<script>
  const total = 3 * 4.5;
  document.getElementById("plain").textContent = "Total: " + total + " <b>EUR</b>";
  document.getElementById("rich").innerHTML = "Total: " + total + " <b>EUR</b>";
</script>

The first paragraph shows the <b> tags literally; the second makes "EUR" bold.

Warning: Never put text typed by a user into innerHTML. If it contains <script> or <img onerror=…>, the browser will run it (a cross-site scripting attack). Use textContent for anything that isn't your own trusted markup.

Dialog boxes

alert() shows a message and pauses the page until the user clicks OK. Its relatives confirm() and prompt() ask a yes/no question or for a line of text:

html · try it yourself
<button id="ask">Ask my name</button>
<p id="answer"></p>
<script>
  document.getElementById("ask").onclick = () => {
    const name = prompt("What is your name?", "Guest");
    if (name !== null) {
      document.getElementById("answer").textContent = "Hello, " + name + "!";
    }
  };
</script>

Dialogs are fine for experiments but block the whole page, so real applications build their own in-page messages instead.

Test yourself

Where does console.log output appear in a browser?

  1. In the page body
  2. In the developer console
  3. In a pop-up dialog
  4. In the address bar

Which property should you use to display text a user typed?

  1. innerHTML
  2. outerHTML
  3. textContent
  4. document.write

What does console.log(["a", 1]) print?

  1. a,1
  2. [ 'a', 1 ]
  3. ["a", 1]
  4. a 1