Data Types in JavaScript
Tags: javascript, webdev, beginners, programming
Published: 2022-08-27 11:44:08

As the name suggests in this piece we are going to discuss different data types in JavaScript.

JavaScript is a **loosely typed language** i.e. variables can be declared anywhere for example within a loop or conditional statements.  It must also be noted that **variables are dynamic in nature** i.e. the type of data stored in them can also be changed. Please look at the code below

```javascript
var a = 1;
console.log(typeof a);

a = "a";
console.log(typeof a);
``` 

```
number
string
```

Before we take a deep dive into different data types it is important to mention that in this article we are only going to discuss primitive data types i.e. data types which are building blocks of other data types (ex: Objects).

In JavaScript there are seven types of primitive data types:

1. Boolean
2. Null
3. Undefined
4. Number
5. BigInt
6. String 
7. Symbol

Before proceeding further it is important to know about the `typeof` operator. Data type of a variable can be checked by using the `typeof` operator as shown in the code below

```javascript
let name = "javascript";
console.log(typeof name);
```

The above code will give the following output

```
string
```

## Boolean
Boolean type variables can only store boolean values i.e. `true` and `false`. See the code below for better understanding.

```javascript
var x = true;
console.log(typeof x);
```

The above code will result in the following output

```
boolean
```

## String
String type variables store textual data for example, a single character or a complete sentence.

```javascript
var w = "word";
var s = "This is a complete sentence";
console.log(typeof w);
console.log(typeof s);
```

The output of the above code will be as follows
```
string
string
```

## Undefined
A variable that has not been assigned a value is of type undefined.

```javascript
var w;
console.log(typeof w);
```

```
undefined
```

## Null
A variable is of type null if it has been intentionally left blank.

There is difference between null and undefined. A variable is of type null if its value has been intentionally left blank where as  variable is of type undefined if its value has not yet been defined.

> Null datatype cannot be identified using `typeof` operator. If a variable containing a null value is passed to a `typeof` operator it will return **object** as output.

```javascript
const a = null;
console.log(typeof a);
```
The output of the above code is given below 

```
object
```

## Symbol
To understand symbols you will need better understanding of the concepts of immutability in Javascript. I will explain this concept in more details in a follow up article for the time being you just need to understand that **symbol data types are immutable and unique**.

## Number
Number type variables can store double precision floating point numbers in the range of {% katex inline%} -2^{1024} {% endkatex %}  to  {% katex inline%} 2^{1024} {% endkatex %}. All the floating point number greater then {% katex inline%} -2^{-1074} {% endkatex %} and less then {% katex inline%} 2^{-1074} {% endkatex %} are converted to {% katex inline%} 0 {% endkatex %}. All the negative floating point integers less then  {% katex inline%} -2^{1024} {% endkatex %} are converted to {% katex inline%} - \infty {% endkatex %} and all the numbers greater then  {% katex inline%} 2^{1024} {% endkatex %} are converted to {% katex inline%} + \infty {% endkatex %}.

Also it must be noted that numbers data type can store integers in the range of {% katex inline%} -(2^{53} -1) {% endkatex %} and {% katex inline%} 2^{53} -1 {% endkatex %}.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cq2ltw5q9n5uoz5izuqp.png)

> If you want to check which if a value can be stored as a number data type, the following refrences could be useful:  
> - `Number.MAX_SAFE_INTEGER`  is the the largest integer value that number can store  
> - `Number.MIN_SAFE_INTEGER` is the smallest integer that number can store. 
> - `Number.MAX_VALUE`  is the largest positive floating point value that number can store.
> - `Number.MIN_VALUE` is the smallest floating point value that number can store.
> - `- Number.MIN_VALUE`  is the largest negative floating point value that number can store.
> - `- Number.MAX_VALUE` is the smallest negative floating point value that number can store.

```javascript
console.log(Number.MAX_SAFE_INTEGER); // largest integer that can be stored in number => 9007199254740991
console.log(Number.MIN_SAFE_INTEGER); // smallest integer that can be stored in number => -9007199254740991
console.log(Number.MAX_VALUE); // largest positive floating point number that be stored in number => 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // smallest positive floating point number that be stored in number => 5e-324
console.log(Number.MIN_VALUE * -1); // largest negative floating point number that be stored in number => -5e-324
console.log(Number.MAX_VALUE * -1); // smallest negative floating point number that be stored in number => -1.7976931348623157e+308
```

## BigInt
BigInt is used to used to store integers which are outside the limit of number data type. Any integer can be converted to a bigInt by using the suffix `n`. See code below for better understanding.

```javascript
const a = 1n;
const b = 1;
console.log(typeof a);
console.log(typeof b);
```
when the above code is run the output will be the following
```
bigint
number
```