JavaScript Tutorial

JavaScript Output

Show a result with console.log, textContent, or an alert. Pick the one that matches the job.

Pick the output that matches the job

JavaScript can write to the console, change the page, or pop a dialog. Those are not the same place. If you log a message and stare at the heading, nothing will happen there.

ToolWhere it appearsUse it for
console.logThe console paneChecking values while you learn
textContentAn element on the pageWhat the reader should see
alertA blocking dialogA short pause, not everyday UI

Click Try it in JavaScript under an example. That opens /javascript/try. The page is on the right. The console is under it.

console.log

console.log prints a value in the console. The page does not change. You can log a string, a number, or several values in one call.

Example

console.log("Hello from the console.");
console.log(2 + 2);
let score = 10;
console.log("score is", score);

This is the fastest way to see what a name holds. Keep it while you debug. Remove it when the page already shows the result.

Write on the page

Find an element, then set textContent. The reader sees the new words. PrefertextContent for plain text. It does not parse tags.

Example

<p id="out">Waiting.</p>
<script>
  const out = document.getElementById("out");
  out.textContent = "The page just changed.";
  console.log("The console can still log too.");
</script>

document.body.textContent = "..." replaces the whole body. Use an id when you want to keep the rest of the page.

alert

alert opens a dialog and waits until the user clicks OK. Nothing else on that page runs until the dialog closes. That makes it a poor default for output.

Example

alert("This stops the page until you click OK.");
console.log("This line runs after the alert closes.");

Use it once to prove a script ran. Then switch to the console or the page. Stacked alerts are hard to dismiss and hide the result you actually care about.

innerHTML is not the first tool

innerHTML writes markup into an element. The browser parses tags. That is useful later. For a beginner string, textContent is enough and safer.

Example

<p id="out"></p>
<script>
  const out = document.getElementById("out");
  out.textContent = "Plain text. Tags stay as words: <b>hi</b>";
</script>

Do not put untrusted text into innerHTML. For words you control, textContent is the default in this tutorial.

Skip document.write

Older examples call document.write. If you run it after the page has loaded, it can wipe the document. Do not use it in new code. Use textContent or the console instead.

A small habit

Log while you figure out a value. Write on the page when the reader should see it. Avoid alertexcept as a one-off check.

Next: statements — the lines that run in order, each ended with a semicolon.

FAQ: JavaScript Output

Common questions about this page.

What is the StudyGrid JavaScript tutorial?

The StudyGrid JavaScript tutorial is a full beginner track: variables, functions, arrays, if/else, the DOM, events, storage, and fetch. Each chapter has copy-and-run examples.

Should I run javascript output examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn javascript output in this JavaScript JavaScript lesson (JavaScript Output).

Is the JavaScript editor the same as Try Python?

No. Try JavaScript is a live page plus a console at /javascript/try. Try Python stays at /try. JavaScript lessons never open the Python editor.

Do I need to install anything to learn JavaScript?

No. Open a chapter, click Try it in JavaScript, and the page and console update in the browser.

Where should I start the JavaScript tutorial?

Start at JavaScript Intro, then Where To, Output, and Syntax. After variables and functions, continue to the DOM and events. Use Next at the bottom of each chapter.

Is the JavaScript tutorial free?

Yes. The JavaScript studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live editor.