IDEA9103 Chap.6 Variables, Constants and Primitive Types
Variables, Constants and Primitive Types
Named bindings make creative quantities readable and reusable. This chapter distinguishes declaration, assignment and reassignment, chooses const for stable bindings and let for deliberate state change, and reviews number, string, Boolean, undefined and null values. It also separates reassigning a binding from changing attributes on the object or element that a constant binding refers to.
What this chapter covers
- 01
Declaration and Assignment
- 02
Let and Reassignment
- 03
Const and Stable Bindings
- 04
Variable Naming Rules
- 05
Primitive Value Categories
- 06
Typeof Inspection
Worked example · free
Audit a canvas configuration
- 1canvasWidth stores 640 once.
- 1particleCount is undefined immediately after declaration.
- 1The first assignment stores 24.
- 1Reassignment replaces it with 36.
Key terms
- Binding
- The association between a program name and its stored value or reference.
- Declaration
- The operation that introduces a named binding to the program.
- Assignment
- The operation that stores a value in a binding.
- Primitive value
- A basic JavaScript value such as a number, string, Boolean, undefined or null.
- typeof
- An operator used to inspect the JavaScript type associated with a value.
Variables, Constants and Primitive Types FAQ
When should I use let rather than const?
Use let when the algorithm deliberately reassigns the binding, such as an animation counter or changing position. Use const for stable references and values whose binding should not be replaced.
What is the difference between null and undefined?
Undefined commonly appears when a binding or argument has no assigned value. Null is an intentional empty value supplied by the programmer, and the captured functions use it to request a generated fallback.
Can a constant DOM element still change?
Yes. Const protects the stored reference from reassignment. Methods can still change attributes on the element that reference points to, which is common in DOM based creative coding.
How can I tell whether a declaration matches the algorithm?
Trace every statement that refers to the binding and circle each assignment. A const declaration matches when the reference is established once and later statements only read it or mutate the referenced object. A let declaration matches when a later event deliberately replaces the stored value. If the trace contains an accidental reassignment, repair that statement rather than weakening the declaration.
The keyword should communicate the state story a reader will actually observe during execution.
Why should width and height have separate variable names?
Separate names preserve separate design decisions. A single vague size value may work for a square, but it hides whether later geometry depends on horizontal extent, vertical extent or both. With canvasWidth and canvasHeight, a trace can change one dimension while holding the other constant and reveal which expressions respond.
The names also make attribute assignment easier to audit because each value carries its intended role into the DOM code instead of relying on its position in a sequence.
What should I record when tracing a changing variable?
Record the binding name, the value after each assignment, its type and the statement that caused the change. Include the observation point because a console log before a reassignment reports a different state from one after it. This small ledger distinguishes declaration, first assignment and later reassignment.
It also exposes unintended coupling: if changing particleCount alters a supposedly fixed canvasWidth, the trace identifies the expression where the two responsibilities became entangled.
When is type conversion a useful repair?
Convert at the boundary where external text enters a calculation and only after naming the type the next operation requires. For example, a text field may supply digit characters that must become a number before arithmetic. Log both the incoming value and typeof result, perform the conversion once, then continue with a clear numeric contract.
Scattered conversions can hide a modelling problem and make later concatenation or addition depend on accidental coercion rather than an explicit creative-coding decision.
How do binding choice and type work together in a trace?
Treat them as separate questions. The declaration keyword describes whether the binding may be reassigned, while the current value determines which operations make sense at that moment. A let binding can hold a number at one step and, although usually undesirable, a string later; const prevents reassignment but does not make the referenced object immutable. Record both the keyword contract and each observed type.
When behaviour is surprising, identify whether the failure came from an unauthorised state change, an unsuitable value type or an operation that assumed a different contract.
Assessment move
Make a two column ledger for each example: binding name and current value. Record every reassignment in execution order and annotate the type. Review names for meaning, reserved words, leading numerals and case consistency. When behaviour surprises you, log both the value and typeof result before converting anything. Explain why each chosen declaration matches the intended state contract.
Working through Variables, Constants and Primitive Types in IDEA9103? Sia is AskSia’s AI Computer Science tutor — ask any IDEA9103 Variables, Constants and Primitive Types question and get a clear, step-by-step explanation grounded in how IDEA9103 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.