Introduction to JavaScript
Table of Contents + −
⚡ What is JavaScript?
The simple answer is JavaScript is:
- A programming language 📜 - a set of instructions that the browser follows
- The language of the browser 🌐 - every browser understands it, with nothing to install
- Event-driven 🖱️ - it runs in response to actions like clicks, typing, and scrolling
- Dynamic ⚡ - it can update the page without reloading it
- Used everywhere 🚀 - it started in the browser and now also runs on servers and apps
🤔 Is JavaScript the same as Java?
No. JavaScript and Java are completely different languages. They only share part of a name, the same way “car” and “carpet” share letters but have nothing to do with each other.
🎯 What does JavaScript do?
JavaScript makes a web page interactive. On a website like YouTube, it allows the page to:
- React to clicks 🖱️: open the sidebar when you click the menu icon
- Update instantly ⚡: turn the like button red without reloading the page
- Respond as you type ⌨️: show search suggestions while you are still typing
- Load content on demand 📡: fetch more videos as you scroll down
The key feature of JavaScript is that it makes web pages respond to the user.
🧩 How HTML, CSS, and JavaScript Work Together
A web page is built from three languages, and each one has a specific job:
| Language | Job | Example on YouTube |
| HTML | Structure and content of the page | The like button exists on the page |
| CSS | How the page looks | The button is styled and positioned |
| JavaScript | How the page behaves | The button turns red when clicked |
Easy way to remember
HTML builds it, CSS makes it look good, and JavaScript brings it to life.
🔧 What problem does JavaScript solve?
In a web page built with only HTML and CSS:
- The content is displayed, but it cannot change
- Buttons and icons do nothing when clicked
- The page cannot respond to the user in any way
JavaScript solves this by:
- Detecting user actions such as clicks, typing, and scrolling
- Updating the page in response, without reloading it
This is what turns a static page into an interactive application.
💻 Your First JavaScript Code
The most common first step in any language is printing a message. The following line prints the word “Hello”:
console.log("Hello");This single line tells the browser to print the text Hello. Let’s break it down piece by piece:
| Part | What it does |
console | A built-in area in the browser where your code can print messages |
console.log(...) | The method that prints whatever you put inside the brackets |
"Hello" | The text to print, written inside quotes |
; | Marks the end of the instruction |
🔍 What is the console?
The console is a panel built into every browser where developers read output and check their code. You can open it by right-clicking the page, choosing Inspect, and clicking the Console tab.
Making a button react
JavaScript can also respond to user actions. The example below changes a button’s text when it is clicked, similar to how the YouTube like button reacts:
<button id="likeButton">Like</button>
<script> // Select the button using its id const button = document.getElementById("likeButton");
// Run this code when the button is clicked button.addEventListener("click", function () { button.textContent = "Liked!"; });</script>Here is what each part of this code does:
<button id="likeButton">Like</button>creates the button on the page and gives it the idlikeButtonso JavaScript can find it.<script>opens the area where the JavaScript code lives.const button = document.getElementById("likeButton");looks up the button by its id and stores it in a variable namedbutton.button.addEventListener("click", function () { ... });tells the button to watch for clicks and run the code inside the function each time it is clicked.button.textContent = "Liked!";is the code that runs on a click; it changes the button’s text fromLiketoLiked!.
This is the core idea of JavaScript: the page listens for a user action and responds to it.
⚠️ Common Mistakes to Avoid
| Mistake | Problem | Solution |
| Wrong capitalisation | JavaScript is case-sensitive, so | Write console.log exactly |
| Missing quotes around text |
| Wrap text in quotes: console.log("Hello") |
| Unclosed quote or bracket | The code does not run | Every quote and bracket needs a matching pair |
// ❌ Wrong - capital CConsole.log("Hello");
// ❌ Wrong - no quotes around textconsole.log(Hello);
// ✅ Correctconsole.log("Hello");Let’s compare the three lines so the difference is clear:
- The first line uses a capital
CinConsole. JavaScript is case-sensitive, so it does not recognizeConsoleand the code fails. - The second line writes
Hellowithout quotes. The browser then treatsHelloas the name of something to look up instead of text, and fails because nothing by that name exists. - The correct line uses lowercase
consoleand wrapsHelloin quotes, so the browser prints the text exactly as written.
Read the error
When something does not work, open the console and read the error message. It usually tells you what went wrong and which line to check.
🔧 Try It Yourself!
- Open any website, right-click the page, choose Inspect, and open the
Console tab. 2. Type
console.log("My first JavaScript!")and press Enter to see your message. 3. Print three separate messages using threeconsole.loglines. 4. Remove a quote from one line, run it, and read the error. Then fix it.
🧩 What You’ve Learned
- ✅ JavaScript is the programming language that makes web pages interactive
- ✅ It runs in the browser and responds to actions like clicks, typing, and scrolling
- ✅ HTML builds the page, CSS styles it, and JavaScript controls its behavior
- ✅ Without JavaScript, a page is static and cannot respond to the user
- ✅
console.logprints messages and is used to check your code - ✅ JavaScript is case-sensitive, so capitalisation matters
🚀 What’s Next?
You now understand what JavaScript is and why interactive websites need it. In the next lesson, we will learn the different ways to run JavaScript so you can start writing it yourself. Let’s continue to Running JavaScript.