JavaScriptLesson 1 of 10Jump to practice

What is JavaScript?

JavaScript makes web pages respond to people. Run your first script and print to the console.

HTML is the structure, CSS is the look, and JavaScript is the behavior. It is a programming language that runs in the browser and can change the page while someone is using it.

Your first script

JavaScript goes inside a <script> element. console.log() prints a message to the console, a log that developers use to check what their code is doing. In these examples the console appears under the result.

Hello, console
Open in editor
<script>
console.log("Hello from JavaScript!");
console.log(2 + 3);
</script>
Result

Changing the page

JavaScript can find an element and change it. Here, clicking the button runs a function, a named group of instructions.

React to a click
Open in editor
<p id="message">Nobody has clicked yet.</p>
<button onclick="sayHi()">Click me</button>

<script>
let clicks = 0;

function sayHi() {
  clicks = clicks + 1;
  document.getElementById("message").textContent =
    "You clicked " + clicks + " times.";
  console.log("Click number", clicks);
}
</script>
Result

What happened

  • document.getElementById("message") finds the element whose id is message.
  • .textContent = ... replaces the text inside it.
  • onclick="sayHi()" tells the button to run the sayHi function when it is clicked.

Practice

Check what you just learned. Fill in the missing code, then answer the quiz.

  1. Exercise 1

    Print the message to the console.

    ("Hello!");

  2. Exercise 2

    JavaScript goes inside which element?

    <>
      console.log("Running!");
    </>

  3. Exercise 3

    Find the element whose id is title.

    const heading = document.("title");

Quiz: 3 questions

1. What is JavaScript's job on a web page?
2. Where does console.log() output appear?
3. What does .textContent = "Hi" do?

Finish the practice to complete this lesson.