JavaScript Datatypes

An opinionated guide for beginners

Created by Tomasz Primke / tprimke (at) gmail (dot) com

The last update: 1st March 2015

Booleans

Simple, isn't it?

Boolean operators

When it comes to booleans, JavaScript is lazy.

It won't evaluate expression, when it's not needed.

Numbers

Simple, isn't it?

Precision of computations

Operations

Please check also the Math object.

Strings

String properties

Type casting

ES 6: string templates

Works in latest Chrome and FireFox.

Don't use it in your code (yet), if you don't use transpilers.

If you don't know, what transpilers are, don't use any ES 6 features.

Undefined

What the heck is it???

undefined is a special JavaScript type and value.

undefined means, that something is, well, undefined.

In another words, it means, that something doesn't exist.

When you should use undefined?

When you'd like to check, whether something is defined (available), or not.

Not sure, what does it mean? Don't worry, it will be shown in the guide on functions.

null

null is a special JavaScript type and value.

It is used to mark, that no value is available.

For example, when you'd like to describe a person in JavaScript, you could use strings for a person's name and surname, and a string for address.

But the adress may be unknown.

So, you could assign null as the address.

In such situations, it's better to use null than undefined.

Arrays

In JavaScript, arrays can store arbitrary number of elements, of any types.

You should use arrays, whenever you have a collection of something.

Properties and methods

No array modification

Please note, that the join method returns a string, not an array.

Simple array modifications

Complex array modifications (1)

Complex array modifications (2)

Don't understand the comparison function? Check some JavaScript reference.

(As for functions, a separate guide will be prepared.)

ES 5.1

All the ES 5.1 methods are available in IE >= 9.

Functional programming

Sorry, I couldn't resist...

Anyway, it's only for ES 5.1

Iterations over arrays (1)

ES 3 - way

Iterations over arrays (2)

ES 5.1 - way

Iterations over arrays (3)

ES 6 - way

I still prefer the forEach function.

Especially with the fat arrow syntax.

But more on this topic in other guides.

Objects

The very heart of Javascript.

Remember, young one: in JavaScript, everything is an object.

What is an object?

Object is a set of properties.

And what is a property?

A property is a pair of a key and a value.

A key is a string.

A value can be any JavaScript value.

(Don't worry, I'll show you an example.)

Example

Accessing object's properties:

Hierarchical access

Adding and removing properties

Usage

Objects are used for modeling entities (like a person, or an address).

Objects are also used as maps (mappings from keys to values).

Iteration over properties

Not so nice...

Fortunatelly for me, I almost never need to iterate over object's properties.

The good news: that's all of the most important stuff.

The bad news: that's all of the most important stuff.

There's nothing more.

Don't worry, though. You can do everything you need with the datatypes you have just learned about.

Date

The Date object is used to represent a single moment in time.

Check also other methods of the Date objects.

JSON

Deserializing

Conclusions

The base datatypes in JavaScript are booleans, numbers and strings.

You should also remember about the special values, like undefined and null.

For single entities, use objects.

For collections, use arrays.

There are also special objects for handling dates and JSON.