Introduction to JavaScript
An introduction to JavaScript: variables, data types, functions, conditionals, arrays, and making web pages interactive in the browser. Runnable examples and a quiz.
Key takeaways
- JavaScript adds behaviour and interactivity to web pages
- Declare variables with let (changeable) or const (fixed)
- Functions package reusable code and can return a value
- if/else and comparison operators let code make decisions
- JavaScript can change the page by responding to events like clicks
The language that makes pages move
HTML builds the structure of a web page and CSS makes it look good — but a page made of only those two just sits there. JavaScript is what makes a page do things: respond to a click, check a form, update a score, or load new content without refreshing. It is the programming language of the web, and it runs right inside your browser.
If you've already styled a page with CSS, JavaScript is the natural next layer.
Trying it out
You don't need to install anything. Open your browser's developer tools (press F12 or right-click and choose Inspect), find the Console tab, and type code there. console.log() prints a value to that console:
console.log("Hello, world!");
You'll see Hello, world! appear. console.log() is JavaScript's main way to display output while you're learning and debugging.
Variables: let and const
A variable stores a value. JavaScript has two main keywords for declaring them:
let score = 0; // can change later
const name = "Maya"; // must stay the same
Use let when the value will change, and const when it should stay fixed. Trying to reassign a const causes an error, which is helpful — it protects values you never meant to change.
let score = 0;
score = score + 10; // fine: score is now 10
console.log(score); // 10
Notice every statement ends with a semicolon ;. JavaScript is forgiving about these, but using them consistently is a good habit.
Data types
JavaScript values come in a few basic types:
let age = 14; // number
let name = "Maya"; // string (text, in quotes)
let isTeen = true; // boolean (true or false)
let pets = ["cat", "dog"]; // array (a list of values)
The + operator adds numbers but joins strings:
console.log(5 + 3); // 8
console.log("Ma" + "ya"); // Maya
console.log("Score: " + 5); // Score: 5
That last line mixes a string and a number — JavaScript turns the number into text and joins them, giving Score: 5.
Functions
A function is a reusable block of code you can call by name. Define it once, use it many times:
function greet(name) {
return "Hi, " + name + "!";
}
console.log(greet("Maya")); // Hi, Maya!
console.log(greet("Sam")); // Hi, Sam!
functionstarts the declaration.greetis the name.namein parentheses is a parameter — a value passed in when you call it.returnsends a value back to wherever the function was called.
Functions let you avoid repeating yourself: write the logic once, then reuse it.
Making decisions
JavaScript uses if, else if, and else, much like the conditionals you may know from Python:
let hour = 14;
if (hour < 12) {
console.log("Good morning!");
} else if (hour < 18) {
console.log("Good afternoon!");
} else {
console.log("Good evening!");
}
The condition goes in parentheses, and the block in braces { }. Comparison operators include <, >, <=, >=, and for equality you should use === (strict equals) and !== (strict not-equals). The strict versions compare both value and type without converting, which avoids confusing surprises — prefer them over ==.
Arrays and loops
An array holds many values in order, and indexes start at 0:
const fruits = ["apple", "banana", "cherry"];
console.log(fruits[0]); // apple
console.log(fruits.length); // 3
A for loop can visit every item:
const fruits = ["apple", "banana", "cherry"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Read the loop header as: start i at 0, keep going while i is less than the array's length, and add 1 to i each time. This is the same "visit every item" idea you may have met in Lists and Arrays.
Making a page interactive
The real power of JavaScript shows when it changes the page. Here is a complete tiny example you can save as an HTML file and open in a browser:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Click Counter</title>
</head>
<body>
<p id="count">Clicks: 0</p>
<button id="btn">Click me</button>
<script>
let clicks = 0;
const button = document.getElementById("btn");
const display = document.getElementById("count");
button.addEventListener("click", function () {
clicks = clicks + 1;
display.textContent = "Clicks: " + clicks;
});
</script>
</body>
</html>
What's happening:
document.getElementById("btn")finds the button on the page.addEventListener("click", ...)runs the function every time the button is clicked.- Inside,
clicksgoes up by one anddisplay.textContentupdates the paragraph's text.
Click the button and the number rises — no page reload needed. That responsiveness is exactly what JavaScript was made for.
The three languages together
You've now met the trio behind every modern website: HTML for structure, CSS for style, and JavaScript for behaviour. Combine them and you can build real, interactive web pages. Revisit Build a Simple Web Page with HTML and try adding a script of your own.
Practice challenges
- Write a function that takes two numbers and returns their sum, then log the result.
- Make an array of three names and use a loop to log each one with "Hello, " in front.
- Add a second button to the click counter that resets the count back to 0.
Quick quiz
Test yourself and earn XP
Which keyword declares a variable whose value will NOT change?
const declares a constant — a value that should not be reassigned. let is used when the value will change.
What does this print? ``` console.log(3 + 4 + "!"); ```
3 + 4 is calculated first as 7, then joined with the string to make the text 7!.
How do you define a function named greet that takes a name?
In JavaScript, a function is declared with the function keyword, a name, parameters in parentheses, and a body in braces.
What is `["a", "b", "c"].length`?
length gives the number of items in an array. There are 3 items, so it is 3.
Why does `===` differ from `==` in JavaScript?
=== is strict equality: it compares value and type without type conversion, which avoids surprising results, so beginners are encouraged to use it.
FAQ
No, despite the similar name they are completely different languages with different uses. JavaScript runs mainly in web browsers to make pages interactive. Java is a separate language used for apps, servers, and Android.
No. Every web browser already runs JavaScript. You can open your browser's developer console (often F12) and type code straight in, or add a <script> tag to an HTML page.
Keep exploring
More in Coding