Introduction
We talked briefly about the fact that JavaScript is a dynamically typed language in the start of this tutorial, but in this article, as well as the following ones, we'll be discussing the various data types you'll run into when working with JavaScript. But why care about data types if the programming language uses dynamic types?
Well, even though JavaScript is not strict when it comes to types, there will still be situations where types matter. For instance, what happens if you multiply "42" (a text string with the number 42) with 2 (the number 2)? A strictly typed language would likely complain about something like this, but JavaScript doesn't really care.
Dynamic types
But let's talk a bit about what a dynamic type is and how you can use it. In a strongly typed programming language, you usually have to declare a variable to be of a specific type, e.g. a string or an integer, and once you have declared the variable, you can't change the type and if you try to assign something of another type to the variable, the compiler will complain. In JavaScript, you don't have to define a type and you can easily assign another type to it afterwards. Here's an example:
let answer = 42;
answer = "The answer to everything: 42";
alert(answer);
Notice that I declare a variable called "answer" and then I assign a number (42) to it. In the next line, I change my mind and assign a string to it, and then I present the answer. JavaScript won't complain about this change at all - you are free to do whatever you want, because the variable is not expected to be a specific type.
Types still matter
But as I already mentioned, just because the types are dynamic doesn't mean that they don't exist. In the background, JavaScript does a lot of stuff for you, so you won't have to worry about types. As an illustration, let's get back to my example of multiplying two numbers, where one of them just happens to be a text string containing the number:
let n1 = "42", n2 = 2;
alert(n1 * n2);
In this case, JavaScript guesses that you are likely trying to multiply two numbers, so even though one of them is actually a text string, the result is still 84. But what if we try to add the numbers instead of multiplying?
let n1 = "42", n2 = 2;
alert(n1 + n2);
You may expect the result to be 44, but instead, you get "422". Why? Because in this case, JavaScript can no longer know that you are trying to do math - instead, it treats both variables as text strings and append them. This is a great example of why types still mater in a dynamically typed language, because you will not get any warnings or complaints about the above example, even though the result may not be what you expected.
Primitive types
Most programming languages distinguishes between primitive types and complex/reference types. JavaScript does the same, where we have several primitive types on one side, and objects on the other side. For reference, these are the primitive data types supported by the JavaScript language:
- bigint
- boolean
- null
- number
- string
- symbol
- undefined
This chapter is all about the primitive types and we'll go through all of them in the next articles. After that, we'll look at some of the built-in objects and how you can define your own complex types.
Summary
JavaScript uses a dynamic type system, which allows you to declare and use variables without having to specify a type. However, as demonstrated in this article, you will have to pay attention to how you use variables of different types together.
In the following articles, we'll dig deeper into the various types JavaScript uses behind the scenes when handling your code, and how you can work with them.