Lesson Objectives
By the end of this lesson, you will be able to:
- Declare variables using
let
andconst
, and understand whyvar
is discouraged. - Distinguish between primitive types and objects in JavaScript.
- Render values into the page and apply simple styling based on data.
1. Why Variables Matter
Variables are named containers for storing data. They let you:
- Label a value so you can refer to it later.
- Update data (with
let
) when it changes. - Protect constants (with
const
) when it shouldnât change.
// A score that will change over time let score = 0 // A fixed maximum value (by convention, constants use uppercase) const MAX_SCORE = 100
Comparing let
, const
, and var
Keyword | Scope | Can Reassign? | Hoisting Behavior | When to Use |
---|---|---|---|---|
let | Block | Yes | Hoisted but uninitialized (Temporal Dead Zone) | Most variables you plan to update |
const | Block | No (immutable reference) | Hoisted but uninitialized (TDZ) | Values that should never be reassigned |
var | Function | Yes | Hoisted and initialized (can lead to bugs) | Avoidâolder code patterns |
Tip: Prefer
const
by default. If you find you need to reassign, switch tolet
.
2. Primitive Types vs Objects
JavaScript has several primitive types and a few object-based types. Understanding the difference is key when copying or passing values.
Type | Example | Notes |
---|---|---|
String | "hello" | Immutable sequence of characters |
Number | 42 | Single Number type for ints/floats |
Boolean | true / false | Logical yes/no |
null | null | Intentional âno valueâ |
undefined | undefined | Variable declared but not yet set |
Object | { x: 10 } | Key/value pairs |
Array | [1, 2, 3] | List-like object |
Function | () => {} | Callable object |
Copying Behavior
-
Primitives: copied by value. Each variable gets its own copy.
let x = 5 let y = x y = 10 console.log(x) // 5 (x is unaffected)
-
Objects: copied by reference. Multiple variables can point to the same object.
let a = { count: 1 } let b = a b.count = 2 console.log(a.count) // 2 (both reference the same object)
3. Rendering Values into the Page
To display JavaScript values in your HTML, target an element and update its properties:
-
Select the container:
const root = document.getElementById('app')
-
Insert text:
root.textContent = `Hello, world!`
-
Style based on data:
// Change color to red if under 18, green otherwise root.style.color = age < 18 ? 'red' : 'green'
Task: Build Your Greeting
In your index.js
, add code that:
-
Declares
const name = 'YourName' let age = 30
-
Renders a greeting into
<div id="app">
, e.g.root.textContent = `Hi, Iâm ${name} and Iâm ${age} years old`
-
Bonus: Use a ternary to color the text:
root.style.color = age < 18 ? 'red' : 'green'
Hint: If you need to update
age
later, youâll needlet
, notconst
.
Reflection Questions
- Why is
const
preferred for values that shouldnât change? - What surprising behavior can
var
introduce inside loops or blocks? - How does copying primitives differ from copying objects when assigning to a new variable?
Next Steps: Try changing age
dynamically (e.g., by reading from an input) and see how the text and color update in real time!