Dynamically typed vs statically typed languages

Haydn Morris
3 min readAug 18, 2018

--

When I first heard the terms statically typed and dynamically typed a confusing image came to my mind. I wondered how the manner in which you type while writing code differs with the language that you are writing in. When I later researched the terms I found that I was somewhat barking up the wrong tree, and that the typed refers in fact to the way in which you declare the types of your variables (int, char, array etc.).

The wrong tree

What’s the difference then?

Declaring variables in JavaScript, Ruby and Python is different to in languages such as C, Java and C++. This is because they are dynamically typed languages. This means that the types of the variables that you declare is enforced at run-time as opposed to compile-time (and vice versa for C, Java and C++).

C:
name[] = "Haydn";
const int eyes = 2;
int age = 24;
JS:
const name = "Haydn";
const eyes = 2;
let age = 24;

As can be seen above with C, the type of the variable must be declared when the variable is declared. For JavaScript, there is no need to declare the type of your variables.

This is because in statically typed languages, the type of the variable refers to the variable itself, and not the value of the variable. This means that when you declare a variable as an integer, only an integer will fit into it for the duration of its existence. If you try to assign a character to it, a TypeError will be thrown and your program will crash.

For dynamically typed languages, the type of a variable refers to the value that the variable contains. This means that if you assign an integer to a variable, the variable is type-enforced as an integer. If you then assign a string to the variable, the enforcement changes to that of a string (although in practice you’d be hard pressed to find a reason to assign a string to a variable that you initially assigned an integer to…).

How does it affect your development?

While these different approaches to declaring variables shouldn’t make too much of a difference to your development flow, there are one or two advantages and disadvantages that come with each:

  • With statically typed languages, there is extra initial effort required when defining your variables as you need to know exactly which types your variables are going to be, and bare in mind if any casting will be required when you use them. This can, however, save time when debugging, because any type errors can be caught earlier on.
  • Dynamically typed languages have a marginally quicker development process when it comes to variable declaration. Type errors will, however, generally be more difficult to locate, so ensuring your code structure is well thought-out is essential!

In conclusion:

Whether a language is statically or dynamically typed is not dependent on how the developer presses the keys on their keyboard, but is concerned with how the types of variables are declared.

Dynamic: types are enforced by the value of the variable, thus the type of the variable can be changed if the value of the variable changes

Static: types are enforced by the variable itself, meaning it cannot be changed and the value that is assigned to it MUST be of the type dictated by the variable

--

--