Javscript: Complex and Primitive Data

Ebba
2 min readApr 4, 2018

--

I joined @podiihq mob session yesterday, for the first time. And we discussed datatypes in Javascript. I learned that there are two different categories within datatypes— so get ready to be enlightened!

The two different categories are:

  1. Primitive data — which is the lowest type of data. All other data is based on primitive data.
  2. Complex data — in order to create complex data, you need primitive data. In other words, complex data is based on primitive data.

So what is included in primitive and complex data?

Primitive Data Includes:

Strings — anything inside a single ‘I am a string’ , or double quotation “I am also a string”

Number — anything that is a number, either with or without a decimal. This is considered a number: 24, and this is also considered a number 24.46

Boolean True or False

Undefined — I have no better way of explaining undefined than showing you this:

var car; // setting a variablecar = undefined; // setting the variable to undefined

You may be asking, why you would want to set a variable to undefined? Well I had the same question, and therefore asked sigu. His answer was that it is useful when logging out a user, since you don’t really want to store any of the users data on the browser storage anymore, once its logged out. Therefore you set the user object on the browser storage to undefined.

Null — is considered a primitive data by some, and by others not. I think it may be because null is considered an object (which apparently is considered a bug in JS, since it should be null), and an object is in return considered a complex data. Messy, messy. But anyhow, null means nothing. So if you set an object to null, you are basically emptying it.

So what is considered Complex data?

Here we can use the typeof operator to find out what kind of complex data there is, and it returns 2 types of data:

  1. Function
  2. Object

Which, if you think about it, makes sense. A function and an object can include any primitive data, but not the other way around. In other words you kind of need primitive data to define a function or an object.

I hope this explanation was helpful! If there are any typos/errors in the text, please let me know — so I can get enlightened :)

--

--