Variables and types
const/let, primitive types, === and handy operators.
Updated
What a variable is
A variable is a name you bind to a value so you can use it later. Think of a label stuck on a box:
const price = 100 // the label "price" → the value 100
const total = price * 2 // you use the label instead of the valueWithout variables you'd have to repeat values everywhere, and you couldn't work with data that changes (user input, server responses).
A variable has three things: a name, a value and a scope (the part of the code where it's visible).
The 3 ways to declare
const |
let |
var |
|
|---|---|---|---|
| Can be reassigned | no | yes | yes |
| Scope | block { } |
block { } |
the whole function |
| Hoisting | yes, but in the TDZ | yes, but in the TDZ | yes, with the value undefined |
| Redeclaring in the same scope | error | error | allowed (dangerous) |
| When to use it | by default, almost always | when the value really changes (counter, accumulator) | never in new code |
const name = 'Ana'
name = 'John' // ✗ TypeError: Assignment to constant variable
let count = 0
count = count + 1 // ✓
if (true) {
let inside = 1
var leaks = 2
}
inside // ✗ ReferenceError — let lives only inside the block
leaks // 2 — var "escapes" the blockconst doesn't mean immutable. It only fixes the binding name → value. If the value is an object or an array, its contents can change:
const user = { name: 'Ana' }
user.name = 'John' // ✓ works — you change the object, not the binding
user = {} // ✗ — you're trying to change the binding(More on this in References and immutability.)
What a type is
Every value has a type — what kind of data it is and what you can do with it. JS is dynamically typed: the variable has no type, only the value does. The same let variable can hold a number now and a string later.
Primitive types
Primitives are simple, immutable values, copied by value.
| Type | Examples | For |
|---|---|---|
string |
'Ana', "text", `Hello ${name}` |
text |
number |
42, 3.14, -1, NaN, Infinity |
any number (one type for integers and decimals) |
boolean |
true, false |
yes / no |
undefined |
undefined |
"has no value yet" — set by JS |
null |
null |
"intentionally empty" — set by you |
bigint |
9007199254740993n |
very large integers |
symbol |
Symbol('id') |
unique keys (rare in apps) |
Objects
Everything that isn't a primitive is an object: objects {}, arrays [], functions, Date, Map… Objects are copied by reference.
null vs undefined
undefined |
null |
|
|---|---|---|
| Who sets it | JS, automatically | you, explicitly |
| Means | an uninitialized variable, a missing property, a function with no return |
"there's nothing here, on purpose" |
typeof |
'undefined' |
'object' (a historical bug) |
Checking the type
typeof 'a' // 'string'
typeof 42 // 'number'
typeof undefined // 'undefined'
typeof null // 'object' ← trap
typeof [] // 'object' ← trap
typeof (() => {}) // 'function'
Array.isArray([]) // true ← this is how you check for an array
x === null // this is how you check for null== vs ===
=== (strict) |
== (loose) |
|
|---|---|---|
| Converts types | no | yes, by rules that are hard to remember |
0 === '' |
false |
0 == '' → true 😬 |
| When | always | practically never |
Truthy and falsy
In if, &&, ||, every value is converted to a boolean. The falsy values (there are only 6): false, 0, '', null, undefined, NaN. Everything else is truthy — including '0', [] and {}.
Operators for missing values
| Operator | Uses the right-hand value when the left is... | Example |
|---|---|---|
|| |
any falsy value (including 0, '') |
name || 'Anonymous' |
?? |
only null / undefined |
limit ?? 10 — keeps 0 |
?. |
— accesses only if it exists | user?.address?.city |
Rule: for default values use ??, not || — otherwise 0 and '' become "missing".
Template literals
const msg = `Hello, ${name}! You have ${count} ${count === 1 ? 'message' : 'messages'}.`Backticks, expressions inside ${}, and they can span several lines.
Summary
- A variable = a name bound to a value;
constby default,letwhen it changes,varnever. - 7 primitive types (copied by value) + objects (copied by reference).
- Always
===;??for default values;?.for safe access.