Web Development Pro | Lesson 021 | JavaScript Values, Variables, and Execution
Web Development Pro · Lesson 21 of 60
Module 3: JavaScript foundations
In this lesson
Prerequisites and learning outcomes
Complete lesson 20 first. This lesson builds on primitive values and script loading. By the end, you can predict expressions and use const or let deliberately in a deferred script. Plan 10–15 minutes for the core explanation and additional time for the worked practice.
Add behavior without replacing the page
JavaScript can calculate values, respond to events, and update a document. The workshop site will eventually filter sessions and show local feedback. Begin with the language itself so you can predict a small calculation before connecting it to a page. HTML and CSS remain useful even when a script fails, so keep essential content in the document rather than making every word depend on JavaScript.
A browser provides JavaScript with web APIs such as the DOM. Node.js runs JavaScript outside the browser and provides different APIs. The core language overlaps, but a script using document will not automatically work in Node. The calculations in lessons 21–25 deliberately use only core language features and console output, making them testable in both environments.
Load an external script deliberately
Save app.js beside index.html and add this tag in head. Defer lets a classic external script download without stopping HTML parsing and executes it after the document has been parsed. Deferred classic scripts retain their document order. Modules, introduced later, have their own loading behavior, so do not mix those rules casually.
<script src="app.js" defer></script>
Open the browser developer console to see console.log output. The console is a debugging surface, not the visitor-facing interface. The supplied page explains where to look. If Node is already installed, running node app.js in that example folder produces the same calculation output. No package installation is required.
Name values clearly
Use const for a binding you do not plan to reassign, and let when reassignment is part of the algorithm. A binding connects a name to a value. Const prevents reassignment of that binding; it does not make every object or array reachable through it immutable. We will explore that distinction after introducing collections.
const workshopTitle = 'Bicycle basics';
const capacity = 12;
let reservedSeats = 7;
reservedSeats = reservedSeats + 1;
const freeSeats = capacity - reservedSeats;
const hasSpace = freeSeats > 0;
console.log(workshopTitle);
console.log(freeSeats);
console.log(hasSpace);
console.log(`${workshopTitle}: ${freeSeats} seats remaining`);
This prints Bicycle basics, then 4, then true, then a sentence containing the same title and count. The assignment updates reservedSeats before freeSeats is calculated. The later const stores the result at that moment; it is not a spreadsheet formula that automatically recalculates if another variable changes afterward.
Distinguish values and types
Strings represent text and use quotes or backticks. Numbers represent numeric values. Booleans are true or false. Undefined often indicates a missing value or a result that was not provided; null is commonly used as an explicit absence value. The language has additional primitive types, but these are enough for the opening examples.
The string '4' and the number 4 can look similar when displayed while behaving differently in expressions. The plus operator can add numbers or concatenate strings. Thus 4 + 2 is 6, but '4' + 2 is '42'. Do not rely on accidental coercion when a value comes from a form. Later lessons will convert and validate input explicitly.
Template literals use backticks and ${...} placeholders to combine text with expression results. They improve readability for sentences with several values. The expression inside a placeholder is evaluated normally; the surrounding template does not change a string into a validated number or make unsafe HTML safe.
Follow execution in order
For this synchronous script, statements run in sequence. Predict each value after each assignment. A variable name is not a historical record: reservedSeats refers to its current value after reassignment. If you need both an old and new value, store them deliberately under names that explain their roles.
JavaScript identifiers are case-sensitive. FreeSeats and freeSeats are different names. A misspelling can cause a ReferenceError rather than selecting the value you intended. Read the first relevant error and its file location instead of changing several unrelated lines. The error message is evidence about what the engine could not do.
Numbers use a floating-point representation, so not every decimal calculation is exact. You will see this again when formatting prices. For simple seat counts within ordinary safe integer ranges, integer arithmetic is straightforward. Do not treat a displayed decimal as a guarantee of exact financial calculation without an appropriate strategy.
Verify the saved script
Save app.js, reload the page, and check that the console shows the expected four outputs once. If nothing appears, verify the script path and look for loading or syntax errors. If old output remains, clear the console or identify which reload produced each message before drawing conclusions.
The local test runs the exact supplied script in Node's isolated VM context and checks its outputs. That establishes the core language behavior for the fixture. It does not prove browser loading or visible DOM updates, which are separate checks. Keeping these scopes distinct makes later debugging much more precise.
Worked practice
Worked exercise 1: Trace reassignment
Task: Start with reservedSeats equal to seven, then add one before subtracting it from twelve. Worked answer: reservedSeats becomes eight and freeSeats becomes four. If you calculate freeSeats before the reassignment, that earlier result is five and does not update itself afterward.
Worked exercise 2: Choose a declaration
Task: The workshop title never changes, but a local click counter will increase. Worked answer: use const for the title binding and let for the counter. This communicates intended reassignment clearly without claiming that const freezes future object contents.
Worked exercise 3: Predict a mixed expression
Task: Compare '12' + 3 with 12 + 3. Worked answer: the first expression produces the string '123'; the second produces the number 15. Similar-looking input needs explicit interpretation, especially when it originated in a form control.
Knowledge check
Question 1
What does const prevent? A) Reassignment of its binding. B) Every possible object mutation. C) A later property assignment through an object held by the binding. D) Mutating an array through another binding to the same array.
Check question 1: answer and explanation
Answer: A. Binding reassignment and object-content mutation are different operations.
Question 2
What is the result of '4' + 2? A) The number 6. B) The string '42'. C) The number 42. D) An automatic validation error.
Check question 2: answer and explanation
Answer: B. Plus concatenates when a string participates in this expression.
Question 3
When does a deferred classic external script execute? A) Before any HTML is parsed. B) Only after all images load. C) After document parsing, in deferred script order. D) As soon as downloading finishes, regardless of document parsing.
Check question 3: answer and explanation
Answer: C. Defer separates download from execution while preserving order for these scripts.
Question 4
Does assigning capacity - reservedSeats create a live spreadsheet formula? A) Yes, because the expression refers to named variables. B) Yes, if the result binding uses const. C) Yes, if either input binding uses let. D) No, it stores the current result.
Check question 4: answer and explanation
Answer: D. Later changes do not automatically recompute the stored number.
Question 5
What does the Node fixture test establish? A) Core script outputs under the test inputs. B) That the deferred script loaded in a browser. C) That the browser console received the same output after a network fetch. D) That an HTML element displayed the computed value.
Check question 5: answer and explanation
Answer: A. Runtime calculations and browser integration have different verification scopes.