Static versus Dynamic Typing

Learn to judge whether a statically typed or dynamically typed language is better suited to an application.

There are two different strategies for dealing with type information in a programming language at the most abstract level: statically or dynamically.

Statically Typed Languages

A language with static types requires that each variable will be assigned a type when it is first declared. The language expects to know the type of each value and uses that information to constrain what values can be assigned to that variable at compile time.

Different static languages have different requirements for how types are assigned. Some languages, like Java, require the type of every variable to be explicitly stated when the variable is declared. Other languages, like TypeScript, allow for type inference. In TypeScript, we can assign a variable with a typical JavaScript assignment like this:

let x = "hello"

Here, TypeScript infers that x is meant to be a string and does not require further information. We don’t have to explicitly declare that x is a string. Later, if we try to say x = 3, the TypeScript compiler will flag this as an error because 3 is not a string.

Get hands-on with 1000+ tech skills courses.