3: Data & Types of Data

Darren Royle
Goto:10
Published in
5 min readMar 2, 2017

Sadly the first and most fundamental place to start is also one of the dullest, but I never said this was going to be easy.

Whenever we use any kind programming, in fact any kind of application in computing, we always require some data to work with. Whether this is information that is relevant to us as human beings (take the animated picture of Data above for example) or whether the data is used to maintain the ‘state’ of the application that is running (the number lives in a game, or a password for a user), it is everywhere.

What is Data?

In short terms it’s a string of bits (1’s and 0’s) that represents something in a given format or ‘type’. So the type could be an animated image, like a GIF file, and the actual content of that file will be the 1’s and 0’s that represent the specific animation. The type determines what the 1’s and 0’s mean in relation to the content it describes, but really this level of understanding is about as far as you need to go. The most important thing is that you understand there are ‘types’ of data ‘GIF’ and there are instances of that data-type ‘animation-of-data.gif’. The same is true in programming, we have types of data ‘number’, ‘line of text’, etc. and we have instances of that data-type ‘1’ or ‘Mary had a little lamb’. Both the content itself and the data-type are important in programming as you might want to add ‘1’ to ‘22’ knowing that they are both numbers, but you would’t want to add ‘1’ to ‘Mary’.

Data Types

In programming there are many different types of data that we can use, and they all work well in specific situations. It is also possible to define a custom data-type which can be a useful method to store data or state that is very specific to your application. So if you wanted an application that worked with recipes you might want a data-type called ‘recipe’ that was specifically designed to hold information about ingredients, method and cooking times. You could then have an instance of a ‘recipe’ called ‘pancakes’ that represented the information relevant to the cooking of pancakes.

Numbers

No prizes for guessing that numbers are important, but like the Eskimos and snow us programmers have lots of ways of representing numbers. The primary methods that you will come across are called Integer which are whole numbers (so no decimals/fractions) and Float which allow you to represent decimals. It may seem odd to have two things where one is so clearly inferior to the other but the nature of data-types is that they help you as the programming convey your intention on a higher level. So if you define ‘Lives’ as an Integer it’s because you don’t want anyone having half, or a quarter of a life, because that’s important to how you’ve made your game.

Strings

Effectively used to represent a ‘string’ of text which is really any amount of text, punctuation including ‘numbers’ that you can think of. It’s called a string because it is stored as a sequential list of ‘characters’ which can be as long as piece of string. When we write a ‘number’ into a ‘string’ we don’t mean number as in the numerical value, but just the ‘text’ representation of that number. Because of this when we ‘add’ strings together like ‘5’ and ‘10’ we actually get ‘510’ because it has mashed the second string into the end of the first. If we keep ‘5’ and ‘10’ as numbers we would obviously get ‘15’ 🐙.

Notice the first line the 5 and 10 are enclosed in quote marks, this shows I want this to be processed as a string, the second command I want them to be processed as numbers, so use without quotes.

Arrays

This is where data types become a bit more functional and useful to us. An array is effectively an ordered list of entries, the entries in the list can be whatever you want — and again you can use whatever type of data you need in the entries. They’re important because they allow us to add ‘order’ to whatever it is we are representing, so if we were putting things into a shopping basket (which is a good way of visualising an array) the order might be the order in which we added things to it.

['Banana', 'Chicken', 'Tomatoes', 'Bread']

The items in my basket are just strings that represent the item I have added, and each item in the basket is separated by a comma and the whole thing is wrapped in square brackets to show it is an array. Now not only do we know all of the things that are in the basket, but also the order in which they were added (which can be useful in a lot of ways). Incidentally if you put this into your console and hit enter, you will find that when your browser prints the information back to you it will allow you to expand the array and see each item (and it’s position):

The first position in any array is always ‘0’, which is mainly because most mathematicians (the fathers of computing) consider ‘0’ as the true first number. You will also notice it shows ‘Array[4]’ which is just telling you that Javascript has returned an array, and it has four items in it — which can be a bit confusing that the position of arrays starts from zero, but we count the length from 1.

Next up we will actually start doing things with data, and get to grips with ‘Variables’, which are effectively instances of data-types but more on that next time.

--

--

Darren Royle
Goto:10
Editor for

Senior Engineer @ DiffBlue LTD. Making magic and technical wizardry, helping you program.