JS Output
In this page:
console.log(value);
document.getElementById("id").innerHTML = value;
document.write(value);
Using innerHTML
innerHTML lets you read or replace the HTML content inside an element, making it the most common way to display dynamic output directly on a webpage.
Example: Using innerHTML
document.body.innerHTML = "<h2>Hello!</h2><p>This text was added with innerHTML.</p>";
Using window.alert()
alert() opens a small popup dialog with a message that pauses the entire page until the visitor clicks OK, making it useful for urgent notices but disruptive for everyday output.
Example: Using window.alert()
window.alert("This is an urgent message!");
Using document.write()
document.write() writes text directly into the HTML document as it's loading, a technique that predates most modern approaches and is now considered outdated for anything beyond simple examples.
Example: Using document.write()
document.write("This text is written directly into the page.");
Using console.log()
console.log() prints messages to the browser's developer console, a tool visible only to developers, making it the standard way to inspect values and debug code without affecting what visitors see.
Example: Using console.log()
console.log("Check the browser console to see this message.");
Choosing the Right Output Method
Picking the correct output method depends on the audience, use innerHTML for content visitors should see, alert only for something urgent that must interrupt them, console.log purely for developer debugging, and avoid document.write outside of simple learning examples.
Example: Choosing the Right Output Method
console.log("Visible only to developers"); // for debugging
document.body.innerHTML = "<p>Visible to visitors</p>"; // for users
- Using alert() for regular output in a finished application, it interrupts the user and feels intrusive outside of debugging or urgent warnings.
- Using document.write() after the page has already loaded, which can wipe out the entire existing page content unexpectedly.
- Forgetting that console.log() output is only visible to developers, never to regular visitors, so it can't replace visible feedback.
- innerHTML changes the actual visible content of an element on the page.
- alert() and console.log() are best for debugging and quick testing, not for regular user-facing output.
- document.write() is mostly a legacy tool and can accidentally erase a page's content if used after loading.
innerHTML, alert, document.write, and console.log are all supported by every browser in use today, from desktop to mobile.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: