Writing Output to the Page
The five ways to display JavaScript output.
Five Ways to Display Output
| Method | Purpose |
|---|---|
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.innerHTML | Sets 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 dialogwindow.("text");// 2. Write into the documentdocument.("value");// 3. Set an element content by iddocument.getElementById("demo"). = "value";// 4. Write to the developer consoleconsole.("text");