โ† All JavaScript lessons

JavaScript / Chapter 01

Values, Variables & Functions

Run JavaScript in the browser, model values with clear variables, use operators and coercion carefully, and organize behavior into reusable functions.

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");
Result
Room310 ready
Check your understanding

Explain 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.

Example
const course = "JavaScript";
let completedLessons = 2;
const active = true;

console.log(typeof course, typeof completedLessons, typeof active);
Result
string number boolean
Check your understanding

Explain 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.

Example
const rawAge = "16";
const age = Number(rawAge);
console.log(age + 1);
console.log(rawAge === age);
Result
17
false
Check your understanding

Explain 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.

Example
function calculateArea(width, height) {
  return width * height;
}

const square = number => number * number;
console.log(calculateArea(4, 3), square(5));
Result
12 25
Check your understanding

Explain 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.

  1. 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.

  2. 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.

  3. 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.

  4. 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

  1. Write a short specification listing inputs, outputs, rules, and failure cases.
  2. Break the work into small functions, queries, modules, or classes appropriate to the language.
  3. Build the smallest working version before adding optional features.
  4. Test normal, boundary, empty, and invalid cases and record the results.
  5. Refactor names and duplication, then write a concise user guide.

Key terms

  • runtime
  • primitive
  • object
  • const
  • let
  • strict equality
  • coercion
  • parameter
  • return