404 Not Found

404 Not Found


nginx

JavaScript Output

The first step in learning programming is usually making your program "talk" — JS has four output methods, each with its own use case.

console.log() — A Developer's Best Friend

console.log() outputs content to the browser's console (F12 → Console). Users can't see it, but developers can't live without it for debugging.

This is the output method you'll use most — by far. Whether you're tracking down a bug or verifying logic, console.log() is your go-to tool.

alert() — Use Sparingly

alert() opens a modal dialog that the user must dismiss by clicking "OK." It interrupts the user's workflow and is almost never used in production — it mainly appears during the learning phase.

Heads up: never put alert() inside a loop during debugging, or you'll have to click "OK" dozens of times to close the dialogs — don't ask me how I know.

document.write() — Testing Only

document.write() writes content directly into the HTML document stream. If you call it after the page has finished loading, it will overwrite the entire page — which is almost never what you want. Use it only for quick experiments.

innerHTML — Updating Page Content

innerHTML is the standard way to "output to the page" — select an element and modify its HTML content. This is the foundation of DOM manipulation, which we'll cover in depth later.

Example: console.log() Output to Console

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>JS Output - console.log</title>
  <style>
    body { font-family: Arial, sans-serif; text-align: center; margin-top: 80px; }
    .tip { background: #e3f2fd; padding: 16px; border-radius: 8px; display: inline-block; color: #1565C0; }
    #output { margin-top: 20px; font-size: 18px; color: #333; white-space: pre-line; }
    button { margin-top: 16px; padding: 10px 24px; font-size: 16px; border: none; border-radius: 6px; background: #2196F3; color: #fff; cursor: pointer; }
    button:hover { background: #1976D2; }
  </style>
</head>
<body>
  <p class="tip">📌 Press F12 to open the console and view console.log output</p>
  <button id="logBtn">Log to Console</button>
  <p id="output"></p>
  <script>
    document.getElementById("logBtn").onclick = function() {
      console.log("Hello, Console!");
      console.log("Current time: " + new Date().toLocaleString());
      console.log("1 + 2 =", 1 + 2);
      var outputEl = document.getElementById("output");
      outputEl.textContent = "Logged 3 messages to the console — press F12 to view!";
      outputEl.style.color = "#4CAF50";
    };
  </script>
</body>
</html>
▶ Try it Yourself

Example: alert() Dialog Output

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>JS Output - alert</title>
  <style>
    body { font-family: Arial, sans-serif; text-align: center; margin-top: 80px; }
    .warn { background: #fff3e0; padding: 14px; border-radius: 8px; display: inline-block; color: #E65100; margin-bottom: 20px; }
    button { padding: 10px 24px; font-size: 16px; border: none; border-radius: 6px; cursor: pointer; color: #fff; margin: 6px; }
    #alertBtn { background: #FF9800; }
    #safeAlertBtn { background: #4CAF50; }
  </style>
</head>
<body>
  <p class="warn">⚠️ alert() blocks the UI — avoid using it in real projects</p>
  <br>
  <button id="alertBtn">Show alert</button>
  <button id="safeAlertBtn">In-page message (recommended)</button>
  <p id="msg" style="margin-top:20px; font-size:20px; color:#4CAF50;"></p>
  <script>
    document.getElementById("alertBtn").onclick = function() {
      alert("This is an alert dialog! You must click OK to continue.");
    };
    document.getElementById("safeAlertBtn").onclick = function() {
      document.getElementById("msg").textContent = "This is an in-page message — no interruption, much better UX!";
    };
  </script>
</body>
</html>
▶ Try it Yourself

Example: innerHTML to Update Page Content

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>JS Output - innerHTML</title>
  <style>
    body { font-family: Arial, sans-serif; max-width: 500px; margin: 60px auto; text-align: center; }
    #content { min-height: 80px; padding: 20px; border: 2px dashed #ccc; border-radius: 8px; font-size: 18px; color: #666; }
    button { margin: 8px; padding: 10px 24px; font-size: 16px; border: none; border-radius: 6px; cursor: pointer; color: #fff; }
    #textBtn { background: #9C27B0; }
    #htmlBtn { background: #e91e63; }
    #clearBtn { background: #999; }
  </style>
</head>
<body>
  <h2>innerHTML Output</h2>
  <div id="content">JS output will appear here</div>
  <button id="textBtn">Output Text</button>
  <button id="htmlBtn">Output HTML</button>
  <button id="clearBtn">Clear</button>
  <script>
    var contentEl = document.getElementById("content");
    document.getElementById("textBtn").onclick = function() {
      contentEl.innerHTML = "This is plain text output";
      contentEl.style.color = "#9C27B0";
    };
    document.getElementById("htmlBtn").onclick = function() {
      contentEl.innerHTML = "<strong style='color:#e91e63;'>Bold text</strong> and <em>italic text</em> — innerHTML can interpret HTML tags";
      contentEl.style.color = "#333";
    };
    document.getElementById("clearBtn").onclick = function() {
      contentEl.innerHTML = "";
      contentEl.style.color = "#666";
    };
  </script>
</body>
</html>
▶ Try it Yourself

Example: document.write() and Its Dangers

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>JS Output - document.write</title>
  <style>
    body { font-family: Arial, sans-serif; max-width: 500px; margin: 60px auto; text-align: center; }
    .danger { background: #ffebee; padding: 14px; border-radius: 8px; color: #c62828; margin-bottom: 20px; }
    button { margin: 8px; padding: 10px 24px; font-size: 16px; border: none; border-radius: 6px; cursor: pointer; color: #fff; }
    #safeBtn { background: #4CAF50; }
    #dangerBtn { background: #f44336; }
  </style>
</head>
<body>
  <p class="danger">⚠️ Calling document.write() after page load will overwrite the entire page! Click the red button to see it in action (refresh to restore)</p>
  <button id="safeBtn">Safe Output (innerHTML)</button>
  <button id="dangerBtn">Dangerous Output (document.write)</button>
  <p id="safeOutput" style="margin-top:20px; font-size:18px; color:#4CAF50;"></p>
  <script>
    document.getElementById("safeBtn").onclick = function() {
      document.getElementById("safeOutput").textContent = "Output via innerHTML — page stays intact!";
    };
    document.getElementById("dangerBtn").onclick = function() {
      document.write("<h1 style='text-align:center; color:red; margin-top:100px;'>The entire page was overwritten!</h1><p style='text-align:center;'>This is the danger of document.write() — refresh to restore</p>");
    };
  </script>
</body>
</html>
▶ Try it Yourself

📖 Summary

  1. console.log() is the primary debugging tool — it outputs to the console where users can't see it
  2. alert() blocks the UI — use it only during learning, never in production
  3. document.write() overwrites the entire page if called after load — only suitable for early experiments
  4. innerHTML is the standard way to update page content and can interpret HTML tags
  5. In real development, 99% of the time you'll use console.log() for debugging and innerHTML for page updates
  6. Each method has its place — choose the right one and you'll work smarter, not harder

❓ FAQ

Q Can users see console.log() output?
A No — it outputs to the developer tools console. Think of it as an instrument panel behind the dashboard: invisible to the driver, but essential for the mechanic. So never put important information only in console.log — users won't see it.
Q What's the difference between innerHTML and textContent?
A innerHTML interprets HTML tags, while textContent treats everything as plain text. For example, setting <strong>bold</strong> — innerHTML renders bold text, but textContent displays the raw tag characters. If you're just changing text, textContent is safer — it won't accidentally execute injected scripts.
Q Why isn't alert() recommended?
A Because it freezes the page — the user must close the dialog before doing anything else. Imagine filling out a form and getting a popup after every field — the experience would be terrible.

📝 Exercises

  1. Beginner: Create a page with a button that, when clicked, uses console.log() to output your name and age.
  2. Intermediate: Create a page that uses innerHTML to output an unordered list (at least 3 items), where each item is a programming language you've learned.
  3. Challenge: Create an "Output Methods Comparison" page with four buttons, each corresponding to one of the four output methods. When clicked, display a description of that method in the same area on the page (skip alert and document.write — simulate them with innerHTML).
100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏