Generally speaking, modern day programming languages fall one of these 2 categories:  dynamically typed vs statically typed.

So what is the difference between a dynamically typed and a statically typed programming language?

Before 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.  (So yes, Java is a statically typed language.)

What is a Statically Typed Language?

Let's start by talking about the concept of “typing”.

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

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 dynamically typed and statically typed programming language is that with dynamic typing, the programmer is not required to specify any types for the variables they create.

How neat is that?

But I'll need to dig in just a bit further to refine my definition of a dynamically typed language. You see, if you don't explicitly set a type, but the programming language can assign a type at compile time, then the language is still considered to be statically typed even though the programmer isn't required to explicitly set a type.

A programming language is said to be dynamically typed if the programmer doesn't have to assign a type explicitly, and a type isn't assigned to the variable until run-time.

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!

Free Java Beginners Course

Start learning how to code today with our free beginners course. Learn more.