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:

javascript

console.log("Hello JavaScript!");

You can also log a calculation or a variable:

javascript

console.log(2 + 3); // 5

const name = "Sajid";
console.log(name); // Sajid

These 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.

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.
JavaScript output in the browser console
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.

javascript

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, and error according to the message.
  • Logs are for developers and operations; visible webpage output is for users.

javascript

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().

javascript

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.

JavaScript browser alert dialog

document.write() writes text or HTML directly into the document while it is loading.

javascript

document.write("Hello World");
document.write("Hello SimplyJavaScript<br>Hello Learners");
Important: Calling 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.

html

<div id="message"></div>

javascript

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.

javascript

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.

javascript

element.innerHTML = "<strong>Hello World</strong>";
// The browser displays bold text.
textContent and innerHTML
PropertyHow it treats the value
textContentPlain text; HTML tags are not parsed.
innerHTMLHTML; 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.

Who should see the output?
AudienceMethodsTypical use
Developersconsole.log(), console.warn(), console.error()Debugging, inspecting values, and diagnosing problems.
UsersDOM, textContent, innerHTMLResults, validation messages, and application content.
Users, brieflyalert()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.

javascript

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.

JavaScript output method comparison
MethodWhere output appearsTypical useRecommendation
console.log()Browser consoleDebugging and inspectionCommon during development
console.warn()/console.error()Browser consoleWarnings and failuresUse with meaningful context
alert()Browser dialogSimple blocking messagesAvoid for normal app UI
document.write()DocumentLegacy/simple demonstrationsGenerally avoid
textContentWebpageUser-facing plain textSafe default for text
innerHTMLWebpageUser-facing HTMLUse 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 innerHTML with untrusted input: Use textContent when you only need plain text.
  • Using logs as the user interface: Developers see console messages; users need visible UI.
  1. Print your name using console.log().
  2. Print the result of 10 + 20.
  3. Print your name, age, and country using one console.log() call.
  4. Create <div id="message"></div>, then use textContent to display "Welcome to JavaScript!".
  5. Predict where console.log("Hello") appears.
  6. What is the difference between element.textContent = "<b>Hello</b>" and element.innerHTML = "<b>Hello</b>"?
Answer: 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.

Reviewed by

SimplyJavaScript Editorial Team

Technical editors and JavaScript educators with hands-on experience building frontend projects, writing learning material, and reviewing tutorials for clarity, accuracy, and beginner-friendly guidance.