You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Variables store data. Data types describe the kind of data. Operators let you manipulate that data. Mastering these three concepts is essential before writing any meaningful JavaScript program.
JavaScript provides three keywords for declaring variables:
| Keyword | Scope | Reassignable? | Hoisted? | Best For |
|---|---|---|---|---|
var | Function | Yes | Yes (initialised as undefined) | Legacy code — avoid in modern JS |
let | Block | Yes | Yes (but not initialised — "temporal dead zone") | Values that change |
const | Block | No | Yes (but not initialised) | Values that stay the same |
var oldWay = "I'm function-scoped"; // avoid in modern code
let counter = 0; // can be reassigned
const PI = 3.14159; // cannot be reassigned
Rule of thumb: Default to
const. Useletonly when you need to reassign. Never usevarin new code.
JavaScript has eight data types, split into primitives and objects.
| Type | Example | Notes |
|---|---|---|
| Number | 42, 3.14, NaN, Infinity | 64-bit floating point (IEEE 754) |
| BigInt | 9007199254740993n | Arbitrary-precision integers |
| String | "hello", 'world', template | Immutable sequence of characters |
| Boolean | true, false | Logical values |
| undefined | undefined | Variable declared but not assigned |
| null | null | Intentional absence of value |
| Symbol | Symbol("id") | Unique, immutable identifier |
Everything that is not a primitive is an object: arrays, functions, dates, regular expressions, and plain objects ({}).
const person = { name: "Alice", age: 30 }; // object
const colours = ["red", "green", "blue"]; // array (a special object)
Use typeof to check a value's type at runtime:
typeof 42; // "number"
typeof "hello"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object" ← historical bug, never fixed
typeof {}; // "object"
typeof []; // "object" ← use Array.isArray() instead
typeof function(){} // "function"
Strings can be created with single quotes, double quotes, or backticks.
const single = 'Hello';
const double = "World";
Template literals (backticks) allow embedded expressions and multi-line strings:
const name = "Alice";
const greeting = `Hello, \${name}! You have \${2 + 3} new messages.`;
// "Hello, Alice! You have 5 new messages."
const multiLine = `Line one
Line two
Line three`;
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.