Your first JavaScript: the console and a script file
Learn what JavaScript does in a web page, connect a script file to your HTML, print messages with console.log, read errors in the browser console, and change a page with one line of code.
🎯 By the end of this lesson you will be able to:
- Explain what JavaScript adds to HTML and CSS
- Link a script file to a page and see its output in the browser console
- Read an error message in the console and fix it
Outline
What you'll learn and why it matters
HTML gives a page its content and CSS its look. JavaScript makes it *do* things: show a menu when you tap a button, check a form before it is sent, add up a basket, load new posts without reloading the page. It is the only programming language that runs in every web browser, which makes it one of the most used and most hired-for languages in the world.
| Layer | Job | Example |
|---|---|---|
| HTML | Content and structure | A button that says *Add to basket* |
| CSS | Look and layout | The button is green with rounded corners |
| JavaScript | Behaviour | Clicking the button adds 1 to the basket count |
Set up a practice folder
You need the same tools as the HTML & CSS course: VS Code (or any code editor) and a browser. Chrome or Edge is best for this course, because the console output in the lessons is shown the way they display it.
- Create a folder called
js-practice. - In VS Code, open the folder and create two files:
index.htmlandscript.js. - Put the HTML below into
index.html, save it, and open it in your browser by double-clicking it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>JavaScript practice</title>
<script src="script.js" defer></script>
</head>
<body>
<h1>JavaScript practice</h1>
<p id="message">Open the console to see what my script says.</p>
</body>
</html>The <script src="script.js" defer> line loads your JavaScript file. defer means *wait until the HTML has loaded, then run the script*, so your code can always find the elements on the page.
Print messages with console.log
Put this line in script.js, save, and refresh the page:
console.log("Hello from JavaScript!");Console (Chrome and Edge)
Hello from JavaScript!
Nothing appears on the page. That is expected: console.log writes to the console, a hidden panel for developers. To open it:
| Browser | Open the console |
|---|---|
| Chrome or Edge on Windows/Linux | Ctrl+Shift+J, or F12 then the Console tab |
| Chrome or Edge on a Mac | Cmd+Option+J |
| Firefox | Ctrl+Shift+K (Windows/Linux) or Cmd+Option+K (Mac) |
You will see *Hello from JavaScript!*. The console is where you check what your code is doing, and where the browser reports errors. Developers keep it open all day.
console.log("I am learning JavaScript.");
console.log(7 + 5);
console.log("7 + 5");Console (Chrome and Edge)
I am learning JavaScript. 12 7 + 5
JavaScript runs your lines from top to bottom. Text in quotes is printed exactly as written; 7 + 5 without quotes is worked out first, so it prints 12.
Change the page with JavaScript
Now the fun part: change what is on the page. Replace the code in script.js with this, and use the Run again button on the preview to see it happen.
const message = document.querySelector("#message");
message.textContent = "This text was written by JavaScript!";
console.log("Page updated");Console (Chrome and Edge)
Page updated
Show the HTML this script works with
<h1>JavaScript practice</h1>
<p id="message">Open the console to see what my script says.</p>Live preview: try it
Console
Nothing logged yet.
Line 1 finds the paragraph with id="message". Line 2 replaces its text. You will learn exactly how this works in lesson 8; for now, notice that a few lines of JavaScript changed the page after it loaded.
Comments
// A one-line comment: JavaScript ignores everything after the two slashes.
console.log("Comments are notes for people"); // They can go at the end of a line too.
/* A comment
over several lines. */Console (Chrome and Edge)
Comments are notes for people
Reading errors
Everyone makes typing mistakes. When JavaScript cannot run a line, it stops and prints a red error in the console. Here *console* is misspelt:
console.log("This line runs");
consol.log("This line has a typo");
console.log("This line never runs");Console (Chrome and Edge)
This line runs
The console shows this error
Uncaught ReferenceError: consol is not defined
Read errors calmly; they tell you a lot:
| Part | Meaning |
|---|---|
Uncaught | Nothing in the code handled the problem, so the script stopped |
ReferenceError | The kind of error: a name JavaScript does not know |
consol is not defined | Exactly which name: you spelt it consol |
script.js:2 | On the right of the console: the file and line number. Click it to jump to the line |
Practice
Guided exercise
Introduce yourself in the console
Write a script that prints three lines in the console: your name, the city you live in, and the result of 2026 minus your birth year (worked out by JavaScript, not by you).
- Use
console.logthree times. - Put your name and city in quotes.
- Write the subtraction without quotes so JavaScript calculates it.
Show the solution
console.log("My name is Asha");
console.log("I live in Pune");
console.log(2026 - 2004);Console (Chrome and Edge)
My name is Asha I live in Pune 22
How to get there: Text goes in quotes and is printed as it is. A calculation without quotes is worked out first, so the console shows the answer, 22.
Your turn
Fix three errors
The starter script has three mistakes: a capital letter in the wrong place, a missing closing quote, and a missing bracket. Fix them so all three lines print.
Console.log("Good morning");
console.log("Today is a great day to learn);
console.log(10 * 3;Show the solution
console.log("Good morning");
console.log("Today is a great day to learn");
console.log(10 * 3);Console (Chrome and Edge)
Good morning Today is a great day to learn 30
How to get there: Console must be lower-case console, every string needs an opening and closing quote, and every ( needs a matching ). Fix one error at a time, save, refresh, and read the next error in the console.
Common mistakes
Looking for console.log output on the page
You see: Nothing seems to happen
Fix: Open the console (Ctrl+Shift+J or Cmd+Option+J). console.log never writes on the page.
Forgetting defer on the script tag in the head
You see: Cannot read properties of null when changing the page
Fix: Add defer, so the script runs after the HTML has loaded.
A wrong file name in src
You see: No output and an error like Failed to load resource or 404 in the console
Fix: Make sure script.js is in the same folder and spelt exactly the same.
Not saving or not refreshing
You see: Old results keep appearing
Fix: Save the file, then refresh the page (F5 or Cmd+R).
Recap
- JavaScript adds behaviour to pages; it runs in every browser.
- Link a file with
<script src="script.js" defer></script>in the head. console.log()prints to the browser console; open it with Ctrl+Shift+J (Cmd+Option+J on a Mac).- Errors show the kind of problem, the name or line involved, and the file and line number.
Key terms
- Console
- A developer panel in the browser that shows messages from scripts and errors.
- Statement
- One instruction in a program, usually ending with a semicolon.
- defer
- A script attribute that runs the script after the HTML has finished loading.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Where does `console.log("Hi")` show its message?
2. What does `console.log(4 + 6)` print?
3. Why add `defer` to `<script src="script.js">` in the head?
4. The console shows `Uncaught ReferenceError: totl is not defined`. What is the most likely fix?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.