≡ Menu

Dynamic Typing vs Static Typing

Generally speaking, modern day programming languages fall into one of two categories:

  1. Dynamic Typing
  2. Static Typing

Dynamic Typing vs Static TypingBefore we can dive into what dynamic or static typing means, we’ll first need to understand what it means by “typing” in general.

Note: This concept actually reaches beyond just JavaScript and Java, but for the scope of this conversation we will be using JavaScript as the example for dynamic typing and Java as the example for static typing.

What is Typing in Programming?

The first place I want to start is to talk a little bit about the concept of typing.

When we say “typing” we’re referring to the concept of applying a “type” to a variable.

I’m going to assume you’re familiar with Java already, but if you’re not, what better place to learn about Java than right here!

So, in Java, we always assign a type to our variables. For example:

String greeting = "Hello!";
Integer someRandomInteger = 100;
Double aDoubleVariable = 2.2;

In all three of these variable declarations above, we have assigned a variable type to each variable. The reason we have done this is because Java is statically typed and therefore we are forced to assign an exact type to the variables before we can use them.

Having a type is important in programming because it allows the language to decide what to do with your code in certain situations.

For example, if you have two Integers and you tell it to add the two together, because they are Integers, the language knows to use addition… however, if you had two Strings and added them together, because they are Strings, the language knows to concatenate them (not add them).

Make sense?

Types are important.

Having said that…

Dynamic vs Static Typing

The difference between dynamic and static typing is that with dynamic typing, the programmer is not required to specify any types for the variables they create.

How neat is that?

This means that with languages that use dynamic typing, you don’t actually have to decide if the variable you’re creating should be a Double or a Float or a String or an Integer etc.

All you need to do is just tell the language that you want to create a variable and you go about your business assigning whatever values you like to it.

So let’s recreate the declaration of those three Java variables using the JavaScript syntax:

var greeting = "Hello!";
var someRandomInteger = 100;
var aDoubleVariable = 2.2;

When we execute this code in JavaScript, the JavaScript interpreter (a.k.a the JavaScript engine) will look at the values that are being assigned to the variables you’ve declared and decide for itself what “type” to assign the variables in the back-end.

Having said that, it’s very important to understand that just because you didn’t have to assign any types to your variables, doesn’t mean that JavaScript doesn’t have types… because JavaScript definitely does have types.

You’ll be introduced to the typeof operator in a couple of weeks. It’s used to determine what type is assigned to your variables.

How Dynamic Typing Works in JavaScript

The concept behind how dynamic typing works in JavaScript is actually pretty straightforward.

The JavaScript engine will choose a type that it feels best describes the data that’s contained inside of your variable. So when you create a variable and assign it the value of "Hello there!", the engine will realize that this is a String and it will assign that as the datatype behind the scenes.

This means that if the JavaScript engine has chosen the String datatype in the back-end, and you were to add another string to this variable, it would know to perform string concatenation and not addition.

So all the same rules of variables that you’re already familiar with, still apply. There’s just a algorithm that works on your behalf to assign a type to your variables.

We’ll go into more details about what implications dynamic typing has to your coding in the next article when we dive further into JavaScript variables.

To Summarize

If you come from a Java background, it might be a little bit of an odd transition into a dynamically typed language like JavaScript, but the most important thing you need to remember is that there are still types that back all of your variables.

You just need to sit back and enjoy your new-found freedom of not having to think about what types to assign to your variables when you declare them! Just declare those variables and walk away 🙂

And as always, be sure to join our email list below and receive a free gift directly to your inbox. You can read more about what I’m giving away as a free gift below this post!

{ 1 comment… add one }

  • Stef Joosten March 22, 2017, 9:31 am

    Let me follow your definition of dynamic typing for Haskell. When I don’t specify the type, Haskell will infer it for me (in most cases). Quite similar to what you describe for JavaScript. So according to this blog, Haskell would be dynamically typed. Yet, everybody out there agrees that Haskell is statically typed. How come?

    The reason is that your definition is flawed. Roughly speaking, the distinction between static and dynamic typing follows the distinction between compile time and run time. Languages like Haskell and Pascal do all their type checking on compile time and are therefore static. Object-oriented languages that allow an object to change its type at runtime (e.g. a “box” becomes a “gift-box” at some arbitrary point in time) cannot even be decided upon at compile time. Therefore, the type system in such languages must be (at least partially) dynamic.

    Note: the distinction between static and dynamic typing works for interpreting languages too. Only “compile time” is usually called “define time” in such languages.

Leave a Comment

Next post:

Previous post: