Web Development - JavaScript Output
JavaScript Output
There are several ways to display data in JavaScript. You can write it to the HTML page, show it in an alert box, or print it to the browser console.
Writing into HTML
You can display output directly inside an HTML element using document.getElementById().
This is the most common way to show results in a web page.
Writing into the Page
You can also write output directly to the HTML document using document.write().
Note: Avoid using document.write() after the page has loaded, because it will overwrite the whole document.
Writing into the Console
For testing or debugging, you can write output to the browser console with console.log().
Open the browser's developer console (usually with F12) to see the output.
Showing Alerts
You can show small pop-up messages using window.alert().
This method is useful for simple messages, but not for real page output.
Summary
innerHTML- write into an HTML element.document.write()- write into the document.window.alert()- display an alert box.console.log()- write to the browser console.
Next, you'll learn about JavaScript Syntax - the basic rules for writing JavaScript code.