# What are Data Types in JavaScript?

Data types represent features of data in a particular programming language, and they are often called values. this article will focus on the basic data types and by the end of this article, you will know the set of data types in JavaScript with examples. 

There are two sets of data types in JavaScript: **primitive** and **object**

According to ```MDN```, primitives are simple and immutable values, meaning they are less complex and cannot be changed.

**Primitive data types**

- Boolean values are either ```true``` or ```false```. They can represent the evaluation of a condition. e.g
```
if (a === 1) {
// do something
} else {
// do another thing
}
``` 
let's imagine ```a``` is a variable defined someplace, then for the ```if``` block to run then ```(a === 1)``` must evaluate to ```true```, ```a``` must be exactly the number 1, note that this evaluation is implicit, meaning that you can't see it. if ```a``` is not equal to 1 then the else block runs. the literal form of Boolean values is  ```true``` and ```false```
- Number is any integer or floating-point number.  e.g let count = 2 or let weight = 60.5
- String is any text that is surrounded by either single or double quotes. e.g let name = 'Olalekan' or let fruits = "berry"
- Null is a type that has only one value, ```null```. e.g let total = null
- Undefined is also a type that has only one value, undefined. e.g let age = undefined 

Although null and undefined can mean an absence of other values, they are different because a variable is undefined when it has been declared but not yet assigned a value. while null represents no value. 

Some primitive values also behave like objects because they have methods. e.g strings, numbers, and Booleans. null and undefined have no methods

**Objects**

 Ok, this part really confused me at first, I will refer to the object data type as reference types. These values include Objects, Functions, Array, RegExp, Date etc. object types are different because they can hold other data types. 

They also have methods and properties attached to them. The object data type can be referred to as reference type because a variable is not actually holding the object data type but a pointer or reference to the value. A variable will only store a reference to an array, function or object, not the value itself 

According to Nicholas Zakas, ```an object is an unordered list of properties and value```. These properties are mostly strings while the value can be any other data type. When the value of a property is a function, it is called a method. 

Reference types can be created either by using the ```new``` constructor e.g 
```
let obj = new Object()

let getInfo = new Function()
``` 
or using their literal form 

```
let obj = {
  name: 'Eren';
  age: 99;
}

function getInfo() {
  return 'something'
}

``` 
But creating a function with the ```new```  keyword is not recommended. 

In the example below, am assigning a pointer to the object to the ```dad``` variable.

```
let dad = { 
  name: "Lee" 
};

let son = user; // son shares the same reference with dad 

console.log(son) // Object { name: "Lee" }
``` 

Changing the object value through one variable will affect the other because they point to the same object in memory. In order to change or access the value of a reference type, we have dot notation 
```
let dad = { 
  name: "Lee" 
};

son.name = "Armin";

console.log(dad) // Object { name: "Armin" }
``` 
and so also is bracket notation


```
let dad = { 
  name: "Lee" 
};

son["name"] = "Armin"

console.log(dad) // Object { name: "Armin" }
``` 

Other than the common object, we also have other specialised object types such as 

 **Array:** an ordered list of numerically indexed values
```
let score = [2, 4, 54, 343];
``` 


**Date:**  represents the date and time  
```
let count = new Date();
``` 


**Function:**
```
function getData(value) {
  return value;
}
``` 

**RegExp:** represents a regular expression 
```
// literal form
var numbers = /\d+/g;
``` 

**Further reading**

[Nicholas Zakas book on OOP](https://nostarch.com/oojs)

[Js.info on object referencing](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjwqbW-rP74AhVErxoKHSepCWwQFnoECEMQAQ&url=https%3A%2F%2Fjavascript.info%2Fobject-copy&usg=AOvVaw3S3DObf980HxIveBQYy46b)


