Variables and data types
Every real program has to store data: a user's name, a cart total, a login status. Variables give that data a name, and data types decide how it behaves, including the famous puzzle of copy by value vs copy by reference.
2.1 The puzzle: how did the original change?
- Copy a number (price) into newPrice and change the copy: price stays 100.
- Copy an object (cart) into newCart and change newCart.items: the original cart.items also becomes 5.
- To understand why, we first need variables and types.
JavaScript
let price = 100;let newPrice = price;newPrice = 120;console.log(price); const cart = { items: 2 };const newCart = cart;newCart.items = 5;console.log(cart.items);Output
> 100> 52.2 What is a variable?
- A variable is a name that points to a value, like a label on a box.
- 3 keywords create a variable: let, const and var.
- let and const are modern (came with ES6); var is the old way you still find in older code.
2.3 let vs const
- let allows reassignment: score goes from 0 to 10 without any problem.
- const does not: assigning gst a new value throws a TypeError immediately.
- Rule: let for a value that will change, const for a value that stays fixed.
JavaScript
let score = 0;score = 10;console.log(score); const gst = 18;gst = 28;Output
> 10TypeError: Assignment to constant variable.2.4 The problem with var
- var ignores the block: city declared inside if is still readable outside (prints Pune).
- let lives only inside its block: reading pin outside gives a ReferenceError.
- var also allows declaring the same name again without any error, so avoid var in modern code.
JavaScript
if (true) { var city = "Pune"; let pin = 411001;}console.log(city);console.log(pin);Output
> PuneReferenceError: pin is not defined| Point | var | let | const |
|---|---|---|---|
| Scope | Function scoped | Block scoped | Block scoped |
| Reassign | Yes | Yes | No (TypeError) |
| Redeclare same name | Yes, no error | No | No |
| Use in modern code | Avoid | When the value changes | Default choice |
2.5 8 data types: 7 primitive + object
- Every value has a type; there are 8 types in total.
- string (text), number (all numbers), boolean (true / false).
- undefined: a value was never given; null: intentionally left empty.
- bigint for very large numbers, symbol for unique keys (rarely used).
- object: normal objects {}, arrays [] and functions.
2.6 Checking types with typeof
- typeof returns the name of the type as a string.
- typeof null is "object": a very old JavaScript bug kept for compatibility.
- Arrays also give "object", so check arrays with Array.isArray.
JavaScript
console.log(typeof "Rahul");console.log(typeof 42);console.log(typeof true);console.log(typeof undefined);console.log(typeof null);console.log(typeof [1, 2]);console.log(Array.isArray([1, 2]));Output
> string> number> boolean> undefined> object> object> true2.7 Copy by value vs copy by reference
- Primitive copy: a new copy of the value is made, so changing newPrice does not touch price.
- An object is stored in one place in memory; the variable only holds a reference (its address).
- Copying cart into newCart copies only the address: both names point to the same object.
- Change it through one name and both see the change: that solves the puzzle.
- Step 1
1.let newPrice = price: a new value 100 is made
- Step 2
2.newPrice = 120: price is still 100
- Step 3
3.const newCart = cart: only the reference (address) is copied
- Step 4
4.cart and newCart point to the same object in memory
- Step 5
5.newCart.items = 5: cart.items is 5 too
Change it through one name, both see it
2.8 Checkpoint: const with an array
- fruits is a const array and we push banana into it: no TypeError.
- const only stops the variable from being reassigned; fruits always points to the same array.
- The data inside the array (or object) can still change.
JavaScript
const fruits = ["apple", "mango"];fruits.push("banana");console.log(fruits);Output
> [ 'apple', 'mango', 'banana' ]2.9 Common mistake: copying an object with =
- newCart = cart is just another name for the same object, so the original changes too.
- Create a new object with spread: { ...cart }; the original stays safe.
- Spread makes a shallow copy; for nested objects use structuredClone (covered later).
JavaScript
// Wrongconst cart = { items: 2 };const newCart = cart;newCart.items = 5;console.log(cart.items); // 5JavaScript
// Rightconst cart = { items: 2 };const newCart = { ...cart };newCart.items = 5;console.log(cart.items); // 22.10 Misconception and rules
- JavaScript does have types, but they belong to the value, not the variable: the same variable can hold a number and later a string.
- This is dynamic typing: flexible, but it causes bugs; TypeScript (later in the course) fixes exactly that.
- Use const by default, let only when you must reassign, no var in new code.
- Meaningful camelCase names like totalPrice or isLoggedIn; use null (not undefined) for an intentionally empty value.
2.11 Interview tips
- let vs const vs var: var is function scoped and can be redeclared; let and const are block scoped.
- const cannot be reassigned, but it does not make an object immutable.
- Primitives are copied by value, objects by reference.
- typeof null is "object" (old bug); use Array.isArray for arrays.