🎓 A1Learner
🔍

JavaScript Intro

JavaScript is the programming language that makes web pages act. The HTML course teaches how to build a page, and JavaScript adds the actions.

A first message

This script sends one message to the console. The console shows the message as one line.

console.log("Hello from JavaScript");

Writing into the page

JavaScript makes the page do something in this snippet. The code finds the demo paragraph, sets its text, and logs the text back.

document.getElementById("demo").textContent = "The page changed";
console.log(document.getElementById("demo").textContent);

Two messages

A script can send more than one message. Each console.log call adds one line to the console.

console.log("First line");
console.log("Second line");

Example

console.log("The script is running");
document.getElementById("demo").textContent = "JavaScript was here";
console.log(document.getElementById("demo").textContent);

Key points

  • Script: a set of instructions that the browser runs
  • Console: a panel in the browser that shows messages from a script
  • Demo paragraph: the paragraph with the id demo on the practice page
  • A script runs when the page loads.
  • A script can read back the text it wrote into a page.

In this example, the first line sends a message to the console. The second line writes new text into the demo paragraph. The last line reads that text back and logs it.

Try it Yourself

Edit the code, then press Run.

Runs in your browser — the preview under the editor is the real result. Nothing is sent to a server. The code runner panel (right) does the same with more room.

Exercise

What does JavaScript do on a web page?