Programming languages / Static vs dynamic typing

Static vs dynamic typing

One small mistake, dividing a word by two, followed through a statically typed language and a dynamically typed one to see when each notices it, and what that timing costs and buys.

Beginner7 steps~6 min
Type checker — reads the code before it runs
Running program — executes one line after another
Type error — a value used in a way its type forbids
// THE SHORT VERSION

In 30 seconds

Every value has a type, such as number or text, and some operations make no sense for some types. Languages differ in when they check this.

Static typing checks before the program runs, so mistakes surface while you write, even in code that rarely runs, and editors can offer accurate suggestions. Dynamic typing checks each operation as it runs: less to write and more freedom, but a mistake waits until that line executes.

Gradual typing, as in TypeScript or Python type hints, lets one program mix both.

Static vs dynamic typing, step by step

STEPS01 / 07

Static or dynamic is about when types are checked. How strictly a language refuses to mix types (strong or weak typing) is a separate question, not covered here.

STEP 01 / 07 Every value has a type
THE MATH
10 is a number "ten" is a string 10 / 2 = 5 "ten" / 2 has no meaning
  1. Step 1: Every value has a type. A type is the kind of thing a value is: a number, a piece of text (a string), true or false. The type decides which operations make sense. Halving the number 10 gives 5. Halving the word "ten" means nothing, and using a value in a way its type does not allow is a type error.
  2. Step 2: Static: checked before running. In a statically typed language such as TypeScript, Java or Rust, the types are part of the code: here n is declared to be a number. A type checker reads the whole program before it runs and refuses the call half("ten"). The mistake is reported while you are still writing, before the program has run at all.
  3. Step 3: Dynamic: checked while running. In a dynamically typed language such as Python, JavaScript or Ruby, names carry no declared type; each value knows its own type as the program runs. Nothing is checked beforehand. The program starts, prints "start", and stops only when it actually tries to divide the string "ten" by 2.
  4. Step 4: Errors found early, or late. A dynamic check only sees the lines that run. Here the mistake hides in a branch used only during a sale, so every ordinary run and test passes, and the error appears weeks later for a real customer. A static checker reads every line, including branches no test reached, and reports it on day one.
  5. Step 5: The price: flexibility. Dynamic typing asks for less. There are no types to write down, a name can hold a number now and a string later, and a list can mix kinds, which makes small scripts and experiments quick. Static typing makes you settle what each thing is up front, and rejects some programs that would in fact have run without trouble.
  6. Step 6: Types help your tools. Because a static checker knows the type of every name without running anything, the editor can use that knowledge as you type. It lists the fields a value really has, underlines a mistake the moment you make it, jumps to a definition, and renames something everywhere it is used without touching look-alikes.
  7. Step 7: Gradual typing: mix the two. You need not pick a side. Gradual typing lets you add types where they pay off and leave the rest dynamic. Python accepts optional type hints: Python ignores them when running, and a separate checker such as mypy reads them beforehand. TypeScript is JavaScript plus types, which are checked and then removed.
VALUES SO FAR
10numbertype
"ten"stringtype
static····when
dynamic····when
late bug····dynamic
trade····cost
tools····static
gradual····both
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

// AFTER

Learn next

Nothing builds on this one yet. See every programming languages concept.

Spotted a mistake on this page?