JavaScript Fundamentals

Uploaded : 3 months ago Updated : 3 months ago beginner

Take The Quiz

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); // 5

Conditions: 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

MethodWhat it doesExample
mapTransform each item[1,2].map(n => n*2)
filterKeep matching items[1,2,3].filter(n => n>1)
findFirst match[1,2,3].find(n => n>1)
includesIs 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?
A function passed to another function to run later — e.g. the arrow function inside forEach.
Arrow or regular function?
Arrow functions are shorter and keep the surrounding this. Use them for short callbacks.
What does return do?
It sends a value back to whoever called the function and ends the function.

Time to build

You have variables, functions, conditions and loops. Put them together on a real page.

Continue to JavaScript in Practice

Comments