Output

Master this topic with zero to advance depth.

Expert Answer & Key Takeaways

Mastering Output is essential for high-fidelity technical architecture and senior engineering roles in 2026.

JavaScript Output

JavaScript can "display" data in different ways. Depending on whether you want to inform the user, debug your code, or manipulate the page structure, you will choose a specific output method.

1. Using innerHTML

To access an HTML element, JavaScript can use the document.getElementById(id) method. The id attribute defines the HTML element. The innerHTML property defines the HTML content.
<p id="demo"></p> <script> document.getElementById("demo").innerHTML = 5 + 6; </script>

2. Using document.write()

For testing purposes, it is convenient to use document.write(). However, using document.write() after an HTML document is fully loaded will delete all existing HTML.
<script> document.write(5 + 6); </script>
[!WARNING] Never use document.write() after the document has finished loading. It will overwrite the entire page.

3. Using window.alert()

You can use an alert box to display data. This is often used for simple notifications or user warnings.
<script> window.alert(5 + 6); // You can also skip the 'window' keyword alert("Hello!"); </script>

4. Using console.log()

For debugging purposes, you can call the console.log() method in the browser to display data. This is the most professional way to check values without interrupting the user experience.
<script> console.log(5 + 6); </script>

5. JavaScript Print

JavaScript does not have any print object or print methods. You cannot access output devices from JavaScript. The only exception is the window.print() method, which prints the current window content.
<button onclick="window.print()">Print this page</button>

Top Interview Questions

?Interview Question

Q:When should you use console.log() instead of alert()?
A:
You should use console.log() for debugging and development. It displays output in the browser console without interrupting the user. alert() should only be used for critical notifications to the end user, as it stops all script execution until it is dismissed.

?Interview Question

Q:What happens if you call document.write() after the page has loaded?
A:
Calling document.write() after the HTML document is fully loaded will completely overwrite the existing HTML content and replace it with the new output.

?Interview Question

Q:Which method is used to print the current webpage?
A:
The window.print() method is used to invoke the browser's print dialog for the current page.

Course4All Engineering Team

Verified Expert

Senior Full-Stack Engineers & V8 Experts

Our JavaScript and engine-level content is developed by a collective of senior engineers focused on high-performance web architecture and 2026 standards.

Pattern: 2026 Ready
Updated: Weekly