JavaScript - Hello World

Overview

Estimated time: 5–10 minutes

Write your first program using the console, an alert, and a simple button interaction.

Learning Objectives

  • Use console.log and alert.
  • Bind a click handler to a button.

Prerequisites

Console Hello

console.log('Hello, World!');

Alert Hello

alert('Hello, World!');

Minimal Page with Button

<!DOCTYPE html>
<html>
  <body>
    <button id="hi">Say Hello</button>
    <script>
      document.getElementById('hi').addEventListener('click', () => {
        console.log('Hello, World!');
        alert('Hello, World!');
      });
    </script>
  </body>
</html>

Exercises

  1. Change the message to include your name.
  2. Log the current time with new Date().toLocaleTimeString().

Checks for Understanding

  1. What’s the difference between console.log and alert in terms of UX?
  2. Where do you see output from console.log?
Show answers
  1. alert blocks the UI and interrupts the user; console.log is non-blocking and visible in DevTools.
  2. In the browser’s DevTools Console panel.