Skip to main content

Dynamic Typing

In Turkish
Dinamik Tipleme
Pronunciation
dy-NAM-ik TY-ping
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/dynamic-typing

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

A type error that only appears at runtime (Python)python
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 * quantity

Readers 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

Spotted a mistake or something missing on this page?Suggest an edit

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

More

Settings