Dynamic Typing
- In Turkish
- Dinamik Tipleme
- Pronunciation
- dy-NAM-ik TY-ping
In short
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.
What is dynamic typing?
In a dynamically typed language such as Python, JavaScript, Ruby or PHP, you write x = 5 without declaring a type, and later x = "five" is allowed too. The interpreter tracks the type of each value as the program runs, and only when an operation doesn't make sense for that value, such as calling a method it doesn't have, does it raise an error.
This makes code quick to write and easy to experiment with. Scripts, prototypes, data analysis and glue code benefit from not having to describe every type upfront. Many dynamic languages also rely on duck typing: if an object has the method you need, you can use it, regardless of its declared class.
The trade-off is that type mistakes surface late, sometimes only on a rarely used code path in production. Large dynamic codebases therefore lean on tests, and increasingly on optional type annotations checked by tools: Python type hints with mypy or Pyright, and TypeScript or JSDoc types for JavaScript.
A common misconception is that dynamically typed languages have no types. Every value still has a type, and a strongly typed dynamic language such as Python refuses to add a number to a string. JavaScript, by contrast, is dynamic and weakly typed: it quietly converts values, so "5" * 2 is 10.
Key takeaways
- Types belong to values and are checked at runtime.
- Python, JavaScript, Ruby and PHP are dynamically typed.
- It is fast for scripts, prototypes and exploration.
- Type errors appear late, so tests and optional type hints help.
- Dynamic doesn't mean untyped: Python is dynamic but strongly typed.
Example
def total_price(price, quantity):
return price * quantity
x = 5
x = "five" # allowed: the variable just points to a new value
print(total_price(9.99, 3)) # 29.97
print(total_price("9.99", 3)) # "9.999.999.99": a string repeated, no error!
print(total_price("9.99", "3")) # TypeError, but only when this line runs
# Optional type hints let tools such as mypy catch it before running
def total_price_typed(price: float, quantity: int) -> float:
return price * quantityReaders ask
Is Python dynamically typed?
Yes. Variables in Python have no fixed type, and types are checked as the code runs. Python is also strongly typed, so it won't silently mix incompatible types, and optional type hints can be checked with tools.
What is duck typing?
Using an object based on what it can do rather than what class it is: if it walks like a duck and quacks like a duck, treat it as a duck. Any object with a read() method can be used where a file is expected, for example.
Is dynamic typing slower?
Often somewhat, because type checks happen while running. Modern just-in-time compilers, such as those in JavaScript engines, narrow the gap a lot by optimizing for the types they actually see.
Often compared
See also
- Static TypingProgramming Fundamentals, p. 53Static 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.
- Data TypeProgramming Fundamentals, p. 14A data type is a classification that tells a program what kind of value a piece of data holds, such as a number or text, and which operations work on it.
- PythonProgramming Languages, p. 26Python is a general-purpose, dynamically typed programming language known for readable syntax and wide use in data science, automation, and web backends.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- InterpreterProgramming Fundamentals, p. 30An interpreter is a program that runs source code directly, step by step, instead of first translating the whole program into a separate executable file.
- Type InferenceProgramming Fundamentals, p. 55Type inference is a compiler feature that works out the type of a variable or expression automatically, so you don't have to write every type annotation.
Spotted a mistake or something missing on this page?Suggest an edit