Type System for Javascript
Published: 2020-12-04 18:53:57

JavaScript is a dynamically-typed language. What this means is that it **performs type checking at runtime**.

Take for instance a language like java or C++ which is a statically-typed language. **Type checking in such language is performed at compile time**. This is very useful as it warns the programmer of all type errors before the code executes successfully. This way, programmers are subjected to writing lesser error codes.


In C++ you would have to define a variable type before using it; You cannot assign a variable with the wrong data type. Hence, it ensures all your variables are checked to make sure they have the right datatype before the code is compiled.

For example, in C++, you would define an integer as:
```javascript
int amount = 200;
```
this is correct and would compile successfully, then run successfully as well because 200 is an integer.

On the other hand, if you tried to declare
```javascript
int amount = “200”;
```
this would fail at compile-time and you would have a compile-time error. Now this error is very useful as it warns you of a bug in your code before the program runs; helping you to fix it and preventing you from pushing a buggy code out for production.

JavaScript has  a different approach to assigning a datatype to a variable. JavaScript allows the kind of data being stored  to define datatype at any given time. 

 For instance in JavaScript, if you define a variable:

```javascript
var amount =   200;
```
then later do:

```javascript
var amount = “200”
```

...both cases would compile successfully and even run successfully.

Now the fact that this compiles and runs successfully does not mean the code run as intended. 

For a real world application, this simple bug could be a huge threat to the life of an organization.




In the first instance, JavaScript would infer the type of the variable amount to be an integer. Later this type inference would get overwritten and JavaScript would infer the amount to be a string in the second instance.

If this code was part of a software handling some financial transactions, were the total amount in a certain user account is to be the sum of all the money coming in to the account. Then the outcome of the total amount in any user account would be wrong.

For example if:

```javascript
var prevAmount = 500;

var currAmount = “200”

var totalAmount = prevAmount + currAmount

console.log(totalAmount)
```





Now the program is expected to have 700 as the total amount but because the prevAmount is an integer and the currAmount is a string, JavaScript performs an implicit conversion on prevAmount, which becomes a string. Then concatenation is done on both instead of expected addition. 


Therefore: 
```
totalAmount = 500200
```


...This gives the user more money than should and puts the organization at risk.






#### IMPLICIT VS EXPLICIT CONVERSION

An implicit conversion is a conversion done by a language to change a datatype to another datatype so they can perform a certain operation. This is very possible in JavaScript because during variable initialization, datatype is not defined. Instead the datatype of a variable is established from the kind of data the variable holds 

Whereas an explicit conversion is a conversion  stated by a programmer to change the datatype of a variable intentionally.

An example of an explicit conversion would be:
```javascript
var totalAmount = prevAmount.toInt() + currAmount.toInt()
```
By performing the explicit conversion, each amount would be converted to an integer before addition is done.   



Now you may ask:
 **"Why can’t I just do this and avoid using a type checker for my code"**. 

Well you can but you would have to remember to do so in every part of your code where necessary  and also, your code would still compile even though you forget to do so in some instance. This means that you are prone to getting unintended results. 


We want to avoid a buggy code compiling as much as possible .
We want all errors fixed before our code finally runs. So that when it finally does, we know for sure we are not having unintended behaviors from type errors.



The differences between static type checking and dynamic type checking can be seen when a type error occurs. In a statically-typed language, type errors occur during compile time. 

Whereas in dynamically-typed languages, the errors occur only once the program is executed. That is, at runtime.


This means that a program written in a dynamically-typed language (like JavaScript or Python) can compile even if it contains type errors that would otherwise prevent the code from running properly.


On the other hand, if a program written in a statically-typed language (like Java or C++) contains a type error, it will fail to compile until the error has been fixed.




The two powerful most used tools that helps you perform type-checking in JavaScript are:

* Flow

* Typescript