Skip to content

Variables & Types

In JavaScript, you can declare a variable with var, let, or const.

KeywordDescription
varOld way of declaring variables (still works but should be avoided because of confusing scope).
letA modifiable variable, limited to the block { } where it is declared.
constA constant variable, cannot be reassigned (but its content can still change if it’s an object or an array).
let name = "Baptiste";
const age = 26;

⚠️ With const, if the value is an object or an array, you can still modify its contents:

const fruits = ["apple", "banana"];
fruits.push("pear");
console.log(fruits); // ["apple", "banana", "pear"]

In JavaScript, there are two main categories:

Primitives are immutable (their value cannot change once created) and are stored directly in memory.

  • String: text
  • Number: number
  • Boolean: true/false
  • Null: empty value
  • Undefined: undefined value
  • Symbol: unique identifier (rare, advanced)
  • BigInt: very large integers
let text = "Hello"; // string
let number = 42; // number
let active = true; // boolean
let empty = null; // null
let unknown; // undefined
let bigNumber = 123456789012345678901234567890n; // BigInt

References point to objects in memory.

  • Object: key/value pairs
  • Array: ordered list
  • Function: also an object in JavaScript
let person = { name: "Alice", age: 30 }; // object
let fruits = ["apple", "banana"]; // array
function greet() { return "Hello"; } // function

Using typeof:

console.log(typeof "Hello"); // string
console.log(typeof 42); // number
console.log(typeof true); // boolean
console.log(typeof null); // object (⚠️ historical bug in the language)
console.log(typeof []); // object
console.log(typeof function(){}); // function

let x = 10;
let y = 5;
let sum = x + y;
console.log("The sum is:", sum); // The sum is: 15

  • Use const by default.
  • Use let only when the value needs to change.
  • Avoid var, except when reading old code.
  • Use typeof to check the type when necessary.