Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14
Chapter 5

Writing Output to the Page

The five ways to display JavaScript output.


Five Ways to Display Output

MethodPurpose
window.alert("text")Shows a pop-up alert dialog to the user.
document.write(value)Writes content into the HTML document. No newline added.
document.writeln(value)Like document.write() but adds a newline after each call.
element.innerHTMLSets the HTML content of a specific element by its ID.
console.log("text")Writes to the browser's developer console. Great for debugging.
window.alert("Hello!");                       // pop-up dialog
document.write("Hello from JS");              // writes into the page
document.writeln("Next line");                // write + newline
document.getElementById("demo").innerHTML = "Hello!";  // set element content
console.log("Debugging value");               // developer console

console.log() is your best friend for debugging — open DevTools (F12) and check the Console tab.

Activity — complete the code

Complete the code correctly to mark this lesson as complete automatically.

Complete the five ways of writing output.

// 1. Pop-up dialog
window.("text");
 
// 2. Write into the document
document.("value");
 
// 3. Set an element content by id
document.getElementById("demo"). = "value";
 
// 4. Write to the developer console
console.("text");