Learn what JavaScript is, how to add scripts to HTML, and write your first lines of JavaScript code.
JavaScript is a programming language that brings web pages to life. While HTML provides the structure and CSS handles the styling, JavaScript adds interactivity — responding to clicks, updating content dynamically, validating forms, creating animations, and much more.
A few important things to know about JavaScript:
There are three ways to add JavaScript to an HTML page:
Inline event handlers — Add JavaScript directly in an HTML attribute like onclick. This is quick but messy and hard to maintain.
<button onclick="alert('Hello!')">Click me</button>Internal script — Place your code inside a <script> tag within the HTML document. This keeps your JavaScript close to the HTML it interacts with.
<script>
document.getElementById('greeting').textContent = 'Hello!';
</script>External script — Write JavaScript in a separate .js file and reference it with a <script src> tag. This is the preferred method for real projects.
<script src="app.js"></script>For the lessons in this course, we will use internal <script> tags so you can see both HTML and JavaScript in one place.
<h1 id="title">Original Title</h1>
<p id="description">This text will change.</p>
<script>
// Select elements by their id
const title = document.getElementById('title');
const description = document.getElementById('description');
// Change their text content
title.textContent = 'Hello, JavaScript!';
description.textContent = 'This text was changed by JavaScript.';
</script>One of the most useful tools for learning JavaScript is the browser console. It lets you see output from your code, test expressions, and debug errors.
To open the console:
F12 or Ctrl+Shift+J (Windows) / Cmd+Option+J (Mac)F12 or Ctrl+Shift+K (Windows) / Cmd+Option+K (Mac)The console.log() function prints a message to the console. It is invaluable for debugging:
console.log('Hello from JavaScript!');
console.log(42);
console.log('The answer is', 40 + 2);You can also use console.warn() for warnings and console.error() for errors. Throughout this course, console.log() will be your best friend for understanding what your code is doing.
Where is the recommended place to put a <script> tag for best performance?