JavaScript Output¶
JavaScript can "display" data in different ways. Unlike print() in Python or System.out.println() in Java, JS output depends on where you want to see the data.
1. Writing into an element (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 content.
2. Writing into the browser console (console.log)¶
This is the most common way for developers to output data for debugging purposes. You view this in the browser's "Inspect Element" -> "Console" tab.
3. Writing into an alert box (window.alert)¶
You can use an alert box to display data. This is intrusive and rarely used in modern professional apps, but useful for quick tests.
4. Writing into the HTML output (document.write)¶
This is mostly used for testing.
Dangerous document.write
Using document.write() after an HTML document is fully loaded (e.g., after clicking a button) will overwrite and delete all existing HTML on the page.