← Back to Week 4
Jan 26, 2026
JavaScript: Variables, Arrays & Objects
Lecture Notes
Slides: JavaScript
Variables & Types
- JavaScript uses dynamic typing - variables adopt the type of their current value
let declares reassignable variables, const declares immutable references
- Basic types: Numbers, Strings, Booleans, Arrays, Objects
- Type coercion automatically converts types during operations (e.g.,
'40' - 2 = 38)
- Use
=== for strict equality (no type coercion), == allows coercion
Truthy & Falsy Values
- Falsy values:
false, 0, '' (empty string), null, undefined, NaN
- Everything else is truthy (including empty arrays and objects)
undefined in conditionals evaluates to false
Arrays
- Declared using literal syntax:
const arr = ['a', 'b', 'c']
- Zero-indexed access:
arr[0] returns first element
- Out-of-bounds access returns
undefined
- Can mix types:
['dogs', 2.5, true, [3,4,0]]
.length property returns array size
.push(value) adds element to end
- Nested arrays for multi-dimensional data:
arr[2][0]
Objects
- Unordered key-value pairs (like dictionaries/maps)
- Declared as:
const obj = {key: 'value', name: 'Alice'}
- Access via bracket notation:
obj['key'] or dot notation: obj.key
- Bracket notation required for variables:
obj[variableName]
- Objects can be nested:
person.favorites.food
Data Tables
- Arrays of objects represent tabular data (like spreadsheets)
- Each object is a row, each key is a column
- Example:
[{name: 'Ada', age: 30}, {name: 'Bob', age: 25}]
Control Flow
- If/else syntax similar to Java
for loops: traditional index-based or for...of for arrays
for...in iterates through object keys
Functions
- Declared with:
function name(params) { return value; }
- No access modifiers or explicit return types
- Parameters don't specify types
Key Concepts
- Dynamic Typing: Variables can hold any type, type determined at runtime
- let vs const:
let for reassignable, const for immutable references
- Type Coercion: JavaScript converts types automatically in operations
- Strict Equality: Use
=== to avoid unexpected type coercion
- Array Methods:
.push(), .length, bracket access
- Object Access: Dot notation for known keys, bracket notation for variables