Variables
Chapter 1 used console.log("Hello, JS!") directly on a literal value. Real code almost always needs to store a value first, under a name, so it can be reused and changed — that's what a variable is. JavaScript has three keywords for declaring one: var, let, and const — only two of which are worth reaching for in modern code.
Declaring a Variable
let name = "Philip"; creates a variable called name, holding the value "Philip". There's no need to declare what type of value it will hold up front — JavaScript figures that out automatically from whatever is assigned.
let vs const — Can It Be Reassigned?
let can be reassigned later; const cannot — trying to assign a new value to a const variable throws an error immediately. The rule of thumb: default to const unless the value genuinely needs to change later, then switch to let. This makes code easier to reason about, since a const guarantees that value never changes anywhere below its declaration.
var is the original way to declare a variable, predating let/const (added in 2015). It has looser, more error-prone scoping rules — a var declared inside an if block leaks out into the surrounding function, unlike let/const, which stay neatly contained to the block they're declared in. Modern code has no good reason to use var.
JavaScript's Core Data Types
Unlike languages with separate integer/decimal types, JavaScript has just one number type covering both. null and undefined are subtly different: undefined means a variable was never assigned anything; null means a value was deliberately set to "nothing" on purpose.
Checking a Value's Type
typeof returns a string naming a value's type — useful while learning, and occasionally in real code when a value's type genuinely isn't known in advance.
Template Literals — Building Strings with Variables
Backticks (`) instead of quotes create a template literal, allowing ${ } to embed variables (or any expression) directly inside a string — far more readable than joining strings together with +.
| Keyword | Reassignable? | Use it when... |
|---|---|---|
| const | No | The default choice — value won't change |
| let | Yes | The value genuinely needs to change later |
| var | Yes | Avoid — kept only for reading old code |
Coding Challenges
Declare a const city with your city's name, and a let temperature with a number. Log both using a single template literal in the form "It is X degrees in Y".
📄 View solutionDeclare let score = 0. Reassign it three times (e.g. += 10 each time), logging score after each reassignment. Then declare const maxScore = 100 and try reassigning it, observing the error in the console.
📄 View solutionDeclare one variable of each core type (number, string, boolean, null, undefined) and use typeof to log each variable's type alongside its value.
📄 View solutionChapter 2 Quick Reference
- const — cannot be reassigned; the default choice
- let — can be reassigned; use only when the value will actually change
- var — older, loosely-scoped; avoid in modern code
- Core types: number, string, boolean, null, undefined
- typeof value — returns a string naming that value's type
- null — deliberately "no value"; undefined — never assigned a value
- Template literals: `text ${variable} more text` — backticks, not quotes
- Next chapter: operators and control flow — if/else, switch, ternary