Chapter roadmap
Run JavaScript in the browser, model values with clear variables, use operators and coercion carefully, and organize behavior into reusable functions. Work through the concepts in order, type every example yourself, and keep a short debugging log that records predictions, errors, fixes, and what each fix taught you.
Learning objectives
- Use the browser console and script files
- Distinguish JavaScript value types
- Choose const and let
- Write and call functions
01.1
Runtime and console
JavaScript runs inside browsers and other runtimes such as Node.js. A script executes from top to bottom, and the developer console displays output and errors. Separate JavaScript files from HTML and load them with defer so the document is available before code runs.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Example<script src="app.js" defer></script>
// app.js
console.log("Room310 ready");ResultRoom310 ready
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
01.2
Values, types, and variables
JavaScript has primitive values including string, number, boolean, undefined, null, bigint, and symbol, plus objects. const prevents reassignment and should be the default; let is appropriate when the binding must change. typeof helps inspect most values, though typeof null is a historical exception.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Exampleconst course = "JavaScript";
let completedLessons = 2;
const active = true;
console.log(typeof course, typeof completedLessons, typeof active);
Resultstring number boolean
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
01.3
Operators, equality, and conversion
Arithmetic and comparison operators create new values. Strict equality === compares without automatic type conversion and should normally replace loose equality ==. Convert deliberately with Number, String, and Boolean, and use Number.isNaN when numeric input may be invalid.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Exampleconst rawAge = "16";
const age = Number(rawAge);
console.log(age + 1);
console.log(rawAge === age);
Result17
false
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
01.4
Function declarations and expressions
Functions name reusable behavior and create local scope. Parameters receive inputs, return sends a result to the caller, and a function should avoid surprising changes outside itself. Arrow functions are concise expressions but do not replace every declaration, especially when this behavior matters.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Examplefunction calculateArea(width, height) {
return width * height;
}
const square = number => number * number;
console.log(calculateArea(4, 3), square(5));Result12 25
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
Chapter assignments
Complete these in order. Later assignments assume that earlier skills are working. Do not only test the sample values; design tests that challenge boundaries, missing values, incorrect types, empty collections, and other likely failures.
- A1.1
Create a profile script that stores and prints at least eight values using suitable types and const or let.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A1.2
Write conversion functions for temperature, distance, and currency-like rates with validated numeric input.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A1.3
Build functions that calculate subtotal, discount, tax, and final total without modifying global variables.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A1.4
Investigate five expressions involving strings, numbers, null, and undefined; predict types and results before running them.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
Chapter project
Console Utility Collection
Console Utility Collection โ Build a browser-console application with at least six small functions, a text menu simulated through prompts, numeric validation, and a final formatted report. Document inputs, returns, and edge cases for every function.
Required process
- Write a short specification listing inputs, outputs, rules, and failure cases.
- Break the work into small functions, queries, modules, or classes appropriate to the language.
- Build the smallest working version before adding optional features.
- Test normal, boundary, empty, and invalid cases and record the results.
- Refactor names and duplication, then write a concise user guide.
Key terms
- runtime
- primitive
- object
- const
- let
- strict equality
- coercion
- parameter
- return