Skip to main content

Dynamic Typing

Dynamic typing checks types at run time, giving flexibility and concise code but catching type errors later and relying more on tests.

Dynamic typing means a program's types are checked while it runs rather than during compilation. Variables are not bound to a fixed type; a name can hold a number now and a string later, and type errors surface only when the offending operation actually executes. Python, JavaScript, Ruby, and PHP are dynamically typed.

How It Works

Values carry their type information at run time, and the runtime checks operations as it encounters them. If code tries to call a method that a value does not support, the error is raised at that moment, not earlier. This enables flexible idioms like duck typing, where what matters is whether an object supports the operations used, not its declared class. Some dynamically typed languages add optional type hints (Python type annotations, TypeScript over JavaScript) that external tools can check without changing the language's runtime behavior.

Why It Matters

Dynamic typing favors speed and flexibility in development. There is less boilerplate, code is concise, and prototyping is fast, which is part of why dynamic languages dominate scripting, data science, and rapid web development. Metaprogramming and generic code can be very expressive.

The cost is that type errors are caught later — potentially in production rather than at build time — so test suites carry more of the burden of correctness. Large dynamic codebases can be harder to refactor safely, which is why optional typing and richer tooling have become popular for big projects.

Related Terms

Static typing checks types at compile time instead. Type system is the underlying framework. Dynamic languages are often run via interpretation, and they lean heavily on unit tests for safety.