DEV Community

loading...
Cover image for Understanding Types; Static vs Dynamic, & Strong vs Weak.

Understanding Types; Static vs Dynamic, & Strong vs Weak.

Cameron Pavey
Originally published at blog.pavey.dev ・3 min read

Generally speaking, when talking about programming languages, most — if not all — languages can be classified by where they fall in a quadrant, with one axis containing Static and Dynamic typing, and the other containing Strong and Weak typing. But what do these terms mean?

Static Typing

Essentially, static typing means that variable types are checked at “compile-time”, or before the code is executed.

Let’s look at an example in TypeScript:

function foo(a: number) {
    if (typeof(a) === 'number') {
        return 'number';
    } else {
        return 'not number';
    }
}

foo(1);
foo('1'); // this will throw a compiler error, because it is not a number
Enter fullscreen mode Exit fullscreen mode

Attempting to pass a string into the function foo after explicitly stating it accepts numbers, would cause it to throw an error such as

Argument of type '"1"' is not assignable to parameter of type 'number'.
Enter fullscreen mode Exit fullscreen mode

Dynamic Typing

On the other hand, dynamic typing means that the variables’ types are checked on the fly, as the code is executed. Consider the following PHP code.

function foo($a) {
    if (gettype($a) === 'integer') {
        return 'integer';
    } else {
        // This will never be evaluated
        return 'not integer';
    }
}

echo foo(1); // integer
Enter fullscreen mode Exit fullscreen mode

Depending on what value is passed into the function foo, the variable $a could technically be any type, and it is only known which one when the code is executed.

On the other axis of our Types quadrant, we have Strong and Weak typing. This is a bit more confusing because there is no universal consensus on what these terms mean, even though they get thrown around a lot. That being said, let’s try to understand them.

Strongly Typed

A lot of — but not all — developers agree that the essence of a strongly typed language is the fact that it converts a variable or value’s type to suit the current situation automatically. This means the "123" is always treated as a string and is never used as a number without manual conversion or intervention. Consider the following Python code:

z = x + y; // This will fail
Enter fullscreen mode Exit fullscreen mode

This fails and returns the following error:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    z = x + y;
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Enter fullscreen mode Exit fullscreen mode

This is because the two variables which we tried to add together and assign to z were different types, and as the error says, adding an int and a string is not supported.

Weakly Typed

A weakly typed language is — as you might expect — the opposite of what is described above. The interpreter or compiler attempts to make the best of what it is given by using variables in ways that might seem confusing at first, but make sense once we understand what they are doing.

This means that in some situations — for example — an integer might be treated as if it were a string to suit the context of the situation.

To demonstrate, let’s try the above example again in a weakly typed language, like JavaScript:

const z = x + y; // 12
Enter fullscreen mode Exit fullscreen mode

Note that this returned 12, instead of 3. This is because rather than performing a math operation as we requested, it treated both values as strings, and concatenated them together, resulting in the string “12”.


Wrapping up

Hopefully, this sheds some light on the minutia of language typing systems for those who do not already know. This is just a fundamental summary, and there is, of course, a lot more to it, but this should hopefully be a good starting point in understanding the finer details of your favourite programming languages. If you found this article helpful, please follow me on Twitter for more content like this!

Discussion (0)