JavaScript Output
Why should you care about JavaScript Output?
Output methods help you see what your code is doing at every step. This is essential for debugging, testing ideas quickly, and building confidence as a developer.
When you write JavaScript, you need a way to see what your code is doing. You may want to check a variable, see whether a function ran, display a result, or investigate a problem.
JavaScript can show information in three common places:
- the browser developer console,
- a browser dialog,
- the webpage itself through the DOM.
We will start with the console because it is the easiest and most useful place to inspect code while learning JavaScript.
Your first output can be one line:
console.log("Hello JavaScript!");You can also log a calculation or a variable:
console.log(2 + 3); // 5
const name = "Sajid";
console.log(name); // SajidThese messages appear in the browser's developer console, not directly on the webpage. That is where developers inspect their code.
console.log() prints information to the browser's developer console. It is the main beginner-friendly way to inspect values while writing and debugging JavaScript.
console.log("Hello World");
console.log(10);
console.log(true);
console.log("Age:", 30);
console.log(2 + 2);
console.log("Name:", "John", "Age:", 30);
const userName = "SimplyJavaScript";
console.log(userName);You can pass several values to one call. This makes it easy to label the information you are inspecting.
Step through real console output examples.
To see console.log() output, open your browser's Developer Tools:
- Press F12, or
- Right-click the page, choose Inspect, and select the Console tab.

console.log("Hello");Developer Console
"Hello"The console is primarily for developers. Visitors will not see a console message as part of the page's visible content.
The console has different methods for different messages:
console.log()shows general information or debugging details.console.warn()highlights a warning or recoverable issue.console.error()reports an error or failure that needs attention.
console.log("Application started");
console.warn("Password will expire soon");
console.error("Unable to load user");All three normally appear in the browser console, with different visual emphasis.
Note: In a browser, window.console and console refer to the same console object. Call a method such as log() or error(); do not call the object itself. Learn more about browser objects in the BOM tutorial.log(), error(), and others to display information in the browser's console. More details will be covered in the BOM part.
console.log() is excellent while learning and debugging. In a production application, logging should be intentional so useful events can be investigated without exposing private information or creating unnecessary noise.
- Never log passwords, access tokens, payment information, or sensitive personal data.
- Use
log,warn, anderroraccording to the message. - Logs are for developers and operations; visible webpage output is for users.
function loadProfile(userId) {
console.info("profile.load.start", { userId });
try {
// load profile data here
console.info("profile.load.success", { userId });
} catch (error) {
console.error("profile.load.failed", {
userId,
message: error.message
});
}
}
loadProfile(42);window.alert() displays a browser dialog that the user must dismiss before interacting with the page. alert() is shorthand for window.alert().
window.alert("Hello!");Alerts are useful for quick demonstrations and testing, but they block interaction. Modern applications usually use custom messages, notifications, or toasts instead.

document.write() writes text or HTML directly into the document while it is loading.
document.write("Hello World");
document.write("Hello SimplyJavaScript<br>Hello Learners");document.write() after the document has loaded can replace the whole document. It is useful for understanding old or very simple examples, but is generally avoided in modern applications.So far, we have mainly shown output to developers. To show a result to the user, JavaScript can find an HTML element and update it through the DOM, the browser's representation of the webpage.
<div id="message"></div>document.getElementById("message").textContent = "Hello JavaScript!";JavaScript finds the element with the message ID and changes its content. This page only introduces the idea; the DOM tutorial covers DOM manipulation in more detail.
textContent inserts plain text into an element. It does not interpret the value as HTML.
const element = document.getElementById("message");
element.textContent = "Hello World";
element.textContent = "<strong>Hello</strong>";
// The <strong> tags appear as text.Use textContent when you want to display text, especially when that text could come from a user or another untrusted source.
innerHTML reads or replaces the HTML inside an element. The browser parses the assigned value as HTML.
element.innerHTML = "<strong>Hello World</strong>";
// The browser displays bold text.| Property | How it treats the value |
|---|---|
textContent | Plain text; HTML tags are not parsed. |
innerHTML | HTML; tags are parsed into elements. |
Security reminder: Do not insert untrusted user input directly into innerHTML. Use textContent for plain text, or sanitize content before inserting HTML.
| Audience | Methods | Typical use |
|---|---|---|
| Developers | console.log(), console.warn(), console.error() | Debugging, inspecting values, and diagnosing problems. |
| Users | DOM, textContent, innerHTML | Results, validation messages, and application content. |
| Users, briefly | alert() | Brief blocking messages in demos. |
Console output is not a replacement for user-facing UI. Developers see logs in the console; users need information rendered in the application.
Template literals use backticks and ${...} placeholders to build readable strings.
const name = "Sam";
const orders = 3;
console.log(`User: ${name}`);
console.log(`Orders: ${orders}`);The expression inside ${...} is evaluated and included in the resulting string. Template literals make formatted console or webpage messages easier to read.
| Method | Where output appears | Typical use | Recommendation |
|---|---|---|---|
console.log() | Browser console | Debugging and inspection | Common during development |
console.warn()/console.error() | Browser console | Warnings and failures | Use with meaningful context |
alert() | Browser dialog | Simple blocking messages | Avoid for normal app UI |
document.write() | Document | Legacy/simple demonstrations | Generally avoid |
textContent | Webpage | User-facing plain text | Safe default for text |
innerHTML | Webpage | User-facing HTML | Use carefully with untrusted input |
- Expecting
console.log()on the webpage: It appears in Developer Tools, not in the page content. - Using
alert()for every message: It blocks interaction and is usually not appropriate for application UI. - Using
document.write()in a modern application: It can replace the document after loading. - Using
innerHTMLwith untrusted input: UsetextContentwhen you only need plain text. - Using logs as the user interface: Developers see console messages; users need visible UI.
- Print your name using
console.log(). - Print the result of
10 + 20. - Print your name, age, and country using one
console.log()call. - Create
<div id="message"></div>, then usetextContentto display"Welcome to JavaScript!". - Predict where
console.log("Hello")appears. - What is the difference between
element.textContent = "<b>Hello</b>"andelement.innerHTML = "<b>Hello</b>"?
console.log() appears in the Developer Console. textContent displays the tags as text, while innerHTML interprets them as HTML.console.log(): Inspects values in the developer console.console.warn(): Reports warnings.console.error(): Reports errors or failures.alert(): Shows a blocking browser dialog.document.write(): Writes into the document and is generally avoided after loading.textContent: Displays plain text safely in an element.innerHTML: Inserts or reads HTML and requires care with untrusted input.- Template literals: Create readable formatted strings.
- Audience: Console output is mainly for developers; DOM output is mainly for users.
What's next? Now that you know how to display and inspect output, learn how to add comments to your JavaScript code in the Comments tutorial.
- What is the purpose of
console.log(), and how does it differ fromconsole.error()andconsole.warn()? - When is
console.log()useful for debugging? - What is
document.write(), and what are its drawbacks? - When might you choose
document.write()over DOM methods? - How does
window.alert()affect user interaction? - What are the advantages and disadvantages of
window.alert()? - What is
innerHTML, and what should you consider when using it? - How does
innerHTMLdiffer from other DOM methods?