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.
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
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 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Spotted a mistake on this page?