Variables and functions
let, const, types, template strings and functions.
JavaScript runs in every browser and, with Node.js, on servers. Open the browser console (F12) and type the examples to try them.
const name = "Ana"; // cannot be reassigned
let score = 10; // can change
score = score + 5;
console.log(`${name} has ${score} points`);Prefer const, use let when the value changes, and avoid the old var.
| Type | Example |
|---|---|
| number | 42, 3.14 |
| string | "text", 'text', ` text ` |
| boolean | true, false |
| undefined | A variable with no value yet |
| null | An intentional empty value |
| object | { a: 1 }, arrays and more |
Equality
CarefulUse === (strict equality), not ==. The loose == converts types: "5" == 5 is true, but "5" === 5 is false.
Functions
function add(a, b) { return a + b; }
const double = (n) => n * 2; // arrow function
console.log(add(2, 3), double(4)); // 5 8Try it
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.