Skip to main content

Side by side

Static TypingvsDynamic Typing

What is the difference between static and dynamic typing?

Updated 2 min read6 differences

In short

Static typing checks types before the program runs, usually with a compiler, while dynamic typing attaches types to values and checks them only at run time.

Static Typing

Static typing means the types of variables and expressions are checked before the program runs, usually by the compiler, so many type errors are caught early.

Read the page on Static Typing

Dynamic Typing

Dynamic typing means types belong to values, not variables, and are checked while the program runs, so a variable can hold a number now and a string later.

Read the page on Dynamic Typing

Static Typing and Dynamic Typing compared

AspectStatic TypingDynamic Typing
When types are checkedBefore running, at compile timeWhile running
VariablesHave a fixed typeCan hold any type over time
Error detectionEarly, in the editor and buildLate, when the code path runs
ToolingPrecise autocompletion and refactoringGood, better with optional hints
Speed of writingMore upfront annotationsVery quick to start
ExamplesJava, C#, Go, Rust, TypeScriptPython, JavaScript, Ruby, PHP

The difference, explained

Statically typed languages, such as Java, C#, Go, Rust and TypeScript, know the type of every variable at compile time. The compiler rejects code that passes a string where a number is expected or calls a method that doesn't exist. Dynamically typed languages, such as Python, JavaScript, Ruby and PHP, let a variable hold any value and discover type mismatches when the line actually runs.

Static types catch whole classes of bugs early and power precise editor features: autocompletion, go-to-definition and safe automatic refactoring across a large codebase. They also act as documentation of what functions expect. Dynamic typing is quicker to write and very flexible, which suits scripts, prototypes, data exploration and code that handles loosely structured data.

The line has blurred. Type inference means statically typed code often needs few annotations, and dynamic languages have added optional type hints checked by tools: TypeScript for JavaScript, type hints with mypy or Pyright for Python. Many teams start dynamic and add types as the codebase grows.

A common misconception is that static means strong and dynamic means weak. Strong versus weak describes how strictly values are converted between types, a separate question: Python is dynamic but strongly typed and refuses to add a number to a string, while C is static but allows many unsafe conversions.

Which one should you use?

Choose Static Typing when…

  • The codebase is large and many people work on it.
  • Bugs are expensive and you want them caught before release.
  • You rely heavily on refactoring and editor tooling.

Choose Dynamic Typing when…

  • You write scripts, prototypes or exploratory data work.
  • Speed of iteration matters more than formal guarantees.
  • You work with loosely structured data and change it often.

Readers ask

Is JavaScript statically or dynamically typed?

Dynamically typed. TypeScript adds static typing on top of it, and the types are removed when it compiles to JavaScript.

Is static typing always better?

No. It brings safety and better tooling at the cost of more ceremony. For small scripts and fast experiments, dynamic typing is often more productive.

What is gradual typing?

Adding optional types to a dynamic language so parts of the code can be typed while the rest stays untyped. TypeScript and Python type hints are examples.

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings