JavaScript ยท Chapter 3 of 55

JavaScript Output

JavaScript has several ways to display output: writing to the browser console, writing into the HTML document, showing alert boxes, or writing to the browser window.

For development and debugging, `console.log()` is by far the most common tool. It prints values to the browser's developer console without disturbing the page.

console.log

console.log() is the developer's best friend โ€” it prints any value, including objects and arrays, to the console for inspection.

Other output methods

`document.write()` writes into the HTML document (rarely used today), `alert()` shows a popup box, and `innerHTML` changes page content directly.

Example 1 (javascript)
console.log("Debug info", 42, true);
Output
Debug info 42 true

console.log accepts multiple comma-separated arguments.

Example 2 (javascript)
document.getElementById("demo").innerHTML = "Hello!";

innerHTML changes the content of an HTML element.

Key points

  • console.log() prints to the developer console.
  • innerHTML changes the content of a page element.
  • alert() shows a blocking popup dialog.
  • document.write() is legacy and rarely used in modern code.
๐Ÿ’ก Note: Open your browser's DevTools (F12) to see console.log output while developing.

๐Ÿ“ Quick Quiz

1. Which method prints to the developer console?

2. Which property changes an element's HTML content?

3. What does alert() do?