JavaScript Fundamentals
Take The Quiz
The building blocks of logic
You can store values. Now learn to act on them: functions, conditions and loops — the heart of every program.
Functions: reusable blocks
A function packages code you can call again and again, optionally with inputs (parameters) and a return value.
function add(a, b) {
return a + b;
}
const arrow = (a, b) => a + b; // same thing, shorter
add(2, 3); // 5Conditions: making decisions
if (score >= 50) {
console.log("Pass");
} else {
console.log("Try again");
}=== vs ==
Use strict equality === (and !==). It compares value AND type, avoiding surprising conversions.
Loops: repeating work
for (let i = 0; i < 3; i++) {
console.log(i);
}
const nums = [1, 2, 3];
nums.forEach(n => console.log(n));Common array methods
| Method | What it does | Example |
|---|---|---|
map | Transform each item | [1,2].map(n => n*2) |
filter | Keep matching items | [1,2,3].filter(n => n>1) |
find | First match | [1,2,3].find(n => n>1) |
includes | Is a value present? | [1,2].includes(2) |
Watch your scope
Variables declared with let/const live only inside their { } block. Reaching for one outside it is a common bug.
Frequently asked questions
What is a callback?
forEach.Arrow or regular function?
this. Use them for short callbacks.