Programming (JavaScript basics) → Lesson 1

Programming What programming is

An introduction to programming concepts: how code becomes behavior and how to think like a debugger.

Learning goals

  • Explain what programming is and how machines follow instructions.
  • Distinguish algorithm, code and program.
  • Describe execution flow: sequential, branching, loops, and function calls.
  • Understand state and determinism in programs.
  • Recognize common error types and apply a debugging mindset.

Core concepts

What is programming?

Programming is writing a set of precise instructions that a machine follows to perform tasks. Those instructions must be unambiguous so the machine can execute them exactly.

Algorithm vs code vs program

Algorithm: a step-by-step method or plan to solve a problem (a recipe). It is language-agnostic.

Code: the textual representation of instructions written in a programming language.

Program: code packaged into a runnable entity that the computer executes (files, scripts, or an app).

How code executes

Most code executes top-to-bottom in sequence. Control structures change that flow:

  • Branching (if/else) chooses different paths based on conditions.
  • Loops repeat work while a condition holds.
  • Function calls jump to a named block of code and return later.

State

State means values stored while a program runs (variables, data structures). State changes over time as code executes and is central to program behavior.

Determinism

Computers are literal and deterministic: the same inputs and operations produce the same outputs (unless randomness, time, or external systems are involved). This predictability makes reasoning and debugging possible.

Errors

  • Syntax errors: mistakes that break the language grammar (missing brackets, typos). These usually prevent the program from running.
  • Runtime errors: errors that occur while running (e.g., dividing by zero, undefined values).
  • Logic bugs: the program runs but produces incorrect results because the algorithm or implementation is wrong.

Debugging mindset

When debugging: isolate the problem, reproduce it consistently, add logging or checks, simplify the case, and test a hypothesis. Work iteratively and keep changes small.

Code examples

Sequential execution

let a = 1;
let b = 2;
let sum = a + b;
console.log(sum); // prints 3

Decision (if / else)

const score = 72;
if (score >= 70) {
    console.log('Pass');
} else {
    console.log('Fail');
}

Simple loop

for (let i = 1; i <= 5; i++) {
    console.log(i);
}
// prints 1 2 3 4 5 each on its own line

Practice tasks

  1. Write code that sets a variable x=10, then logs x*2. Expected output: 20
  2. Implement a decision that logs "even" or "odd" for a given number. Test with 7 → expected: "odd".
  3. Write a loop that prints the numbers 0 through 3. Expected output: 0 1 2 3 (one per line).
  4. Create a function that takes a name and returns "Hello, <name>". Calling with "Ava" should return "Hello, Ava".
  5. Using an array [2,4,6], compute and log the sum. Expected output: 12
  6. Introduce a deliberate logic bug (e.g., off-by-one) and describe how you'd isolate and fix it.

Common mistakes & debugging checklist

  • Check for syntax errors first — they often prevent execution.
  • Reproduce the failing case with minimal input.
  • Add console logs near suspect code to inspect values and flow.
  • Simplify the code: reduce to a small snippet that still fails.
  • Read error messages carefully — they usually point to the cause.
  • Write small tests for expected outputs.