# The Different Data Types

30.05.2016

Episode #3 of the course A quick introduction to JavaScript by Rich Armstrong

Now that you’ve played around a bit, let me tell you about some different data types like numbers, strings, arrays, objects, and booleans. All pretty weird names, right?

Strings
These are pieces of text. A string is signified by a starting and ending quote—either both single or both double (as in `‘a string’` or `“a string”`.) You can add them together; this is called concatenation or interpolation. And how long is a piece of string, you may ask? Try writing `'a kinda long string by my standards'.length` in your console and then try `'a shorter string'.startsWith('a')` and `'nice string'.startsWith('a')`.

Numbers
Numbers don’t have any quotes: 42, 53, 41.23, -99.1. These are all numbers in JavaScript. Try to add them, multiply them, divide, and subtract them. Use brackets. Here, try this:

```console.log(42 + 34);
console.log(2 * 3);
console.log(99 / -72);```

And then you can do a little more with JavaScript’s built-in Math object. Try these:

```Math.round(41.34)
Math.max(23, 42)
Math.round(Math.random()*100)```

Run the last one over and over and you’ll get a different number anywhere from 0 to 100. Pretty cool, huh?

Booleans
Weirdest name of them all. Basically, its value can either be true or false. Try this:

```var richIsGoodLooking = true;
console.log('is rich goodlooking? '+ richIsGoodLooking);```

Now change the value of `richIsGoodLooking` to `false` and rerun it. What’s really cool about booleans is that they can be used in if-else statements! We’ll cover more of this in future lessons, but try this to get your hands dirty:

```if (7 > 3) {
console.log('Yeah it sure is bigger!');
}```

Objects
Booleans, numbers, and strings are primitive data types. Objects are more complex. They can contain other data types, including other objects, functions, and arrays (more on that later), and they have a key and value system, like so:

```var person = {
name: 'Rich Armstrong',
male: true,
age: 28,
favouriteFoods: ['pizza', 'bunny chows', 'curry'],
magic: true
};```

Notice the commas after each key-value pair (except for the last)! You can access an object’s properties with dot notation `person.name` or with bracket notation `person['name']` and set them with `person.age = 95;` or `person['age'] = 95;`. You can even set properties that don’t exist yet. Try it out.

Arrays
Arrays are like objects, but their keys are numbers starting from zero. Arrays are lists, and they can contain any data type, but mostly the list is made up of the same data type. Like this:

`var shopping = [’milk’,’eggs’,’lotto ticket’,’awesome muesli’];`

You can access array items with bracket notation `shopping[2]` and set them with `shopping[3] = 'average muesli';` Arrays are zero-based, which means to access the first item, you find out what item zero is `shopping[0]`, and to access the last item, you find out what item at the array’s length minus one is `shopping[shopping.length - 1];`. Write it. Play around.

Dates and Time
You get the current date like so:

`var currentDate = new Date();`

And to get info about the date, you access its properties `currentDate.getFullYear()` and `currentDate.getTime()` are just two examples. The last will give you the amount of milliseconds since 1970. JavaScript measures time in milliseconds. Research dates to have more fun with them. And see what the console will auto-fill if you write a date and press .

Remember: Download Google Chrome for Desktop and Sublime Text 3 to make the most of these lessons. And remember to open up your JavaScript Console: Alt+Cmd+J (OS X) or Alt+Ctrl+J (Windows).

Recommended book

“A Smarter Way to Learn JavaScript: The new approach that uses technology to cut your effort in half” by Mark Myers

Share with friends