Static and Dynamic Typing

To further develop my programming skills, I have looked at learning new languages and technology. While deciding on what to focus my time and energy on learning I looked at what is different about the languages and one difference that comes up is Static or Dynamic typing within the language. Throughout the Flatiron school Bootcamp, the main languages we learned were Ruby and JavaScript, both of these have dynamic typing.

Photo by Margot RICHARD on Unsplash

There are several other languages with static typing, like Java or C. The difference between static typing and dynamic typing is that with static typing the variables you want to use need to be initiated and declared before you use them. Whereas with dynamic typed we do not need to declare variables before we use them we can simply name a variable and assign it a value. In code it would be:

Java (Static typed):
int num;
num = 2;
JavaScript (Dynamic typed):
const num = 2;

It’s only a short example with one variable but we can see how our code with the Dynamic typed language would be shorter, easier to read, and less code needing to be written to use a variable. There is a disadvantage however to using dynamic typed languages that I originally didn’t think of when looking at the two different types until I read more about the differences in the following articles:

That disadvantage is that if you make a mistake when trying to change or edit a previously used variable in a dynamic typed language it won’t cause an error, it will work as if there is no error and that you were simply declaring and assigning a new variable. With static typed something like this would be avoided as the variable with the typo in it would throw an error as it hadn't been previously declared. Here is a simple example where it is easy to see where the problem occurs:

JavaScript(dynamic typed):let num = 2

numm = num * 2

Here the idea is we simply wanted to double the value of num and have the new value be assigned back to num. Unfortunately, since the spelling mistake of num to numm now we have a new variable numm that is double the original num and we have two variables. With this example, it's somewhat easy to spot the mistake but I’m sure you could imagine with longer code and lots of variables it could be harder to notice. Again a mistake like this wouldn't be possible to make with static typed.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store