We left off with a few unanswered questions in our last post, the largest of which being, “what exactly is an untyped language and what does it mean for JavaScript?”. In this post, that is the very topic that we will cover but first we should have a little talk about data and types in general.

bigdataA data type (or “type”) is simply a classification of the attributes of the data that we are entering into the program. In a computer, you have the RAM (random access memory) and the CPU (Central Processing Unit), when you write a program which manipulates data, you need to classify what that data will look like so the ram may store it efficiently and the CPU will be able to use it correctly. You wouldn’t want your computer to try to perform a computation on the wrong data types (multiplying an integer by a string for example.) In even more extreme cases where you are using composite typed data such as an object, you can run into even worse problems without a static type system or dynamic type inference (the compiler infers what the data is during runtime.)

A very simple way of understanding a type is to talk about material things in the real world. For example, if we think about apples and bananas, we know that they both have a specific and distinct set of characteristics despite them both being a type of fruit.   We can reason about apples and bananas individually as apples and bananas respectively or we can reason about both of them as fruit together. If we then try to start reasoning about something that isn’t a fruit, apple, or banana; say we throw a bunch of trucks into our collective pile of things; it becomes much harder for us to reason about all of the objects together.

Ultimately, the problem occurs because there are no categories like “fruit” that can cover all three of these items anymore except for extremely general and useless categories such as objects, items, or matter etc.   As human beings, we can dynamically assign a “type” or category to everything that we encounter in our world. In English, at least, we are constantly using a mixture of static types and type inference to define data we encounter from day to day.

JavaScript’s Type System and TypeScript

typescript

JavaScript is a dynamically typed language (or a weakly typed language without an explicit type system); it doesn’t use static types to define its data. This means that the programmer doesn’t explicitly define what their data is before using it in a program. It also means that the burden of aligning the right types of data is on the programmer. Say you build a function that takes two strings and outputs a boolean. Because JavaScript has no type inference or type system the program can accidently put any data type into that function and cause an error.

datatypesFor those of you that are new to programming, it may seem like a trivial thing to categorize your data but it is very important.  In fact, JavaScript’s lack of a solid type system became such a problem that a superset programming language called TypeScript was developed to add one to the language. TypeScript is a very powerful abstraction that can be used on top of normal JavaScript to help minimize errors and make code much more readable.

JavaScript might be a language without a static type system but it has dynamic types. In JavaScript, you have six types of data: Objects, Numbers, Strings, Booleans, Null and Undefined. Thus far our “Hello, World!” variable assignment only made use of a string but as we get further into this language we will cover all of the different types in JavaScript.

In the next post, we will talk about Numbers as well as some of the different built-in operations that we can perform on them with JavaScript.