Output in JavaScript
In JavaScript, "output" means displaying information to the user or to the developer. There are multiple ways to show output depending on the situation — let’s explore them all!
1. console.log() – Developer Console Output
This is the most common way developers test and debug their code.
console.log("Hello, world!");
- Displays output in the browser's developer console.
- Great for debugging and checking values.
- Not visible to regular users on the webpage.
To see it:
- Open your browser → Right-click → Inspect → Console tab.
2. document.write() – Writing to the Page
This method writes directly into the HTML document.
document.write("This text appears on the page.");
- Simple and direct.
- Not used in real projects because it overwrites the whole page if used after it loads
3. alert() – Popup Message
This creates a popup box that displays a message.
alert("This is an alert box!");
- Easy way to show a quick message.
- Can be annoying for users if overused.
- Blocks interaction until the user clicks "OK"
4. Output to HTML Elements
You can display output by changing the content of an HTML element using innerHTML.
HTML:
<p id="output">
JavaScript:
document.getElementById("output").innerHTML = "Hello from JS!";
- Best way to show output on the page without overwriting anything.
- Commonly used in real web applications.