appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

Variables and functions

Lesson 1 of 310 minBeginner

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.

TypeExample
number42, 3.14
string"text", 'text', ` text `
booleantrue, false
undefinedA variable with no value yet
nullAn 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 8

Test yourself

Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.

1. Which keyword declares a variable that cannot be reassigned?
2. What is "5" === 5?
Hintconsole.log(...)
4. What is (n) => n * 2 called?

Key terms