JS Types & Variables
JavaScript uses variables to hold data in memory. ES6 modernized this by bringing us let and const to replace the problematic var. Let's break down how they work.
let & const vs var
var is function-scoped. This means if you declare a var inside an if block, it can leak out to the surrounding function. It also undergoes Hoisting, meaning its declaration is moved to the top of the code, resulting in an undefined value if accessed early.
let and const are block-scoped (bounded by {}). They solve the leakage problem. Furthermore, if you try to use them before declaration, you hit the Temporal Dead Zone (TDZ) and get a ReferenceError. Always prefer const for values that won't change, and let for variables that will be reassigned (like loop counters).
Primitive Data Types
JavaScript distinguishes between Primitives and Reference Types (Objects). Primitives are immutable values passed by value. There are 7 primitive types:
- String: Text surrounded by quotes.
- Number: Integers and floating point numbers.
- Boolean:
trueorfalse. - Undefined: A variable declared but not assigned.
- Null: Explicitly empty or non-existent value.
- BigInt: Very large integers.
- Symbol: Unique identifiers.
View Full Transcript+
In this lesson transcript, we explore the historical context of ES6 (ECMAScript 2015) which introduced block scoping. By minimizing the scope of variables, developers reduce cognitive load and prevent accidental overwrites. We also dive into how memory handles primitive types directly on the call stack, versus reference types which are stored in the heap.
