JS Input & Output
Browser dialogs (`alert`, `prompt`, `confirm`) for input, and the five ways JS shows output — `console.log`, `alert`, `document.write`, `innerHTML`, `window.print`.
JavaScript talks to users in two directions:
Input — three blocking browser dialogs let JS read a value the moment it's needed:
alert(msg)— pops a modal with an OK button (returnsundefined).prompt(msg, default?)— text-input dialog, returns the typed string (ornullif cancelled).confirm(msg)— OK / Cancel dialog, returnstrue/false.
Output — five common channels for showing results:
console.log()— writes to the DevTools / Node terminal. The developer's go-to.alert()— pops a modal dialog. Loud and intrusive, but unmistakable.document.write()— writes directly into the HTML document (during page load).element.innerHTML— replaces the inner HTML of any DOM element. The standard for real apps.window.print()— opens the browser's print dialog.
Most production code uses
innerHTML(or React's equivalent).console.logis for debugging,alertis for emergencies,document.writeis a relic. Browser dialogs (alert/prompt/confirm) block the page — modern UIs use<dialog>or in-app modals instead.
javascriptalert("Hello!"); // OK button — returns undefined const name = prompt("Name?"); // text input — returns string | null const ok = confirm("Sure?"); // OK / Cancel — returns boolean
javascript// Tiny demo — runs in any browser console const name = prompt("What's your name?", "Aarav"); if (name && confirm(`Hi ${name}, ready to start?`)) { alert(`Welcome, ${name}!`); } else { console.log("User cancelled."); }
| Method | Where it shows | Use case |
|---|---|---|
console.log() | Browser DevTools / Node terminal | Debugging, structured inspection |
alert() | Modal dialog over the page | Quick "are you sure?" confirmations |
document.write() | Inline in HTML (during load only) | Tutorials, never in production |
el.innerHTML | Inside a DOM element | Real apps — dashboards, forms, lists |
window.print() | OS print dialog | Print invoices, certificates, receipts |
Step 1. Create a file output.html:
html<!DOCTYPE html> <html> <body> <h2>JavaScript Output Demo</h2> <p id="demo">Loading…</p> <script> // 1. Console — open DevTools (F12) to see this console.log("Hello from console.log"); // 2. innerHTML — replaces the <p> contents document.getElementById("demo").innerHTML = "Hello, Codekilla!"; // 3. document.write — appends to the page during load document.write("<p>Written by document.write</p>"); // 4. alert — uncomment to see the popup // alert("Hello, alert!"); </script> </body> </html>
Step 2. Open the file in a browser (double-click, or right-click → Open with).
Output (visible on the page):
JavaScript Output Demo
Hello, Codekilla!
Written by document.write
Output (in DevTools console after pressing F12):
Hello from console.log
- The browser parsed the HTML top to bottom.
console.logprinted to the developer console (a separate panel).getElementById("demo").innerHTML = "..."swapped the paragraph's content with the new string.document.writeappended new HTML while the page was still loading. Calling it after load nukes your whole page — that's why it's avoided in real apps.
| Property | Renders | Risk |
|---|---|---|
innerHTML | Parses string as HTML (tags, styles, even <script>) | XSS if untrusted input is interpolated |
textContent | Pure text, no HTML parsing | Safe by default — prefer for user-supplied data |
javascriptel.innerHTML = "<b>Hi</b>"; // renders bold el.textContent = "<b>Hi</b>"; // renders the literal characters
| Call | Use |
|---|---|
console.log(...) | Standard print |
console.warn(...) | Yellow warning |
console.error(...) | Red error with stack trace |
console.info(...) | Info message |
console.table(arr) | Pretty-print arrays / objects |
console.dir(obj) | Tree view of an object |
console.group("name") / .groupEnd() | Nested log blocks |
console.time("t") / .timeEnd("t") | Measure elapsed ms |
console.assert(cond, msg) | Log only when cond is false |
javascriptconsole.table([{ id: 1, city: "Mumbai" }, { id: 2, city: "Delhi" }]); console.time("loop"); for (let i = 0; i < 1e6; i++); console.timeEnd("loop"); // loop: 4.2 ms
Don't want to set up an HTML file? Use Codekilla's online JavaScript compiler — console.log output appears directly in the run pane.
- Calling
document.write()after the page loads — it overwrites your entire page with the string. - Trusting
innerHTMLwith user input — pasting raw HTML opens XSS attacks. UsetextContentfor user-supplied strings. - Missing the DevTools panel —
console.logonly appears once you press F12 and click the Console tab. - Confusing
console.log()withprint()— JavaScript has no built-inprint()for stdout (Python lingo). It'sconsole.log. alert()flooding — callingalertin a loop locks the browser. Reserve it for one-off confirmations.- Treating
prompt()return as a number — it's always a string (ornull). Wrap withNumber(...)before math. - Leaving
alertdebug calls in production — embarrassing.
- Three Outputs — Build an HTML page that uses
console.log,alert, andinnerHTMLto display "Hi {your_name}". Hint: target a<p id="demo">. - Safe vs Unsafe — Write a script that takes
userInput = "<img src=x onerror=alert(1)>"and inserts it into the page usinginnerHTML, then again usingtextContent. Observe what happens. Hint: open DevTools to see the difference. - Pretty Table — Console-table the array
[{name: "Aarav", score: 92}, {name: "Bhavna", score: 87}]. Hint:console.table(arr). - Prompt Calculator — Use two
prompt()calls to read numbers from the user, thenalert()the sum. Hint: convert withNumber(...)first. - Confirm Guard — Show a
confirm("Delete this item?")and onlyconsole.log("deleted")when the user clicks OK. Hint: returns a boolean.
💡 Think Like a Programmer: Default to
textContentwhen displaying any user-supplied string. Switch toinnerHTMLonly when you specifically need to render trusted HTML — and even then, sanitise first with a library like DOMPurify. For real input UI, prefer in-app modals or<dialog>over the blocking browser dialogs.
Quick recap quiz?
We'll generate 5 MCQs from this lesson and check your understanding instantly. Takes ~30 seconds.
Program
const name = prompt("Name?", "Aarav");
console.log("Hello", name);