JavaScript Tutorial
Modern JavaScript (ES2023) for beginners — variables, functions, arrays, async/await, and the DOM.
JavaScript powers the web. This tutorial covers the modern language you’ll actually use in 2026 — no outdated var patterns.
Running JavaScript
# In the browser console, or with Node.js:
node script.js
Declaring values
const name = "Ada"; // cannot be reassigned
let count = 0; // can change
// var is legacy — avoid it
Use const by default; reach for let only when rebinding is needed.
Functions and arrow syntax
const add = (a, b) => a + b;
function greet(name = "world") {
return `Hello, ${name}!`;
}
Working with arrays
const nums = [1, 2, 3, 4, 5];
const doubled = nums.map(n => n * 2); // [2,4,6,8,10]
const evens = nums.filter(n => n % 2 === 0); // [2,4]
const sum = nums.reduce((acc, n) => acc + n, 0); // 15
const found = nums.find(n => n > 3); // 4
Async with promises and await
async function getUser(id) {
const res = await fetch(`/api/users/${id}`);
if (!res.ok) throw new Error("Not found");
return res.json();
}
getUser(1)
.then(user => console.log(user))
.catch(err => console.error(err));
The DOM
const btn = document.querySelector("#load");
btn.addEventListener("click", () => {
document.querySelector("#out").textContent = "Loaded!";
});
Modules
// math.js
export const square = x => x * x;
// app.js
import { square } from "./math.js";
console.log(square(4));
Tip: Always use ES modules (
import/export) for new code. They work in browsers and Node alike.