Playing around with datatypes in JS
Published: 2022-01-02 15:31:46

One of the most important component of a programming language is its datatypes. They can be looked upon as a building block of any programming language. In this article we will play around with datatypes in JS and explore more about it!
In JavaScript one of the most interesting feature about variables is that , they are not particularly assosiated with any specific value type and they can assigned and reassigned a value any time.That is why is said that JS is a loosely typed and dynamic language. 
![datatype1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/80o9a701ztn38cy4fmn9.JPG
)
**There are primarily two types in JavaScript:-**
1.Primitives
2.Objects

**Primitives**
Primitives are immutable.There are 7 different Primitives
1.Boolean
2.Null
3.Undefined
4.Number
5.BigInt
6.String
7.Symbol

**Objects** 
Objects are nothing but collection of data in form of key value pairs.

**typeof operator**
The typeof operator helps us to find the type of any unevaluated operand. For eg,

![datatype 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t7n76noy0i4nb7jd85r8.JPG)
Function and arrays are also objects in JavaScript!
![datatype 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m9fwu8ihrnlf3umyuwm6.JPG)
**BigInt**
BigInt is a datatype which deals with operations on huge value of integer numbers. BigInt allows to work with numbers beyond the limit of Number primitive.
BigInt is represented by suffix n.


![bigint](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1224hm7gqjcg5jghxz1q.JPG)
 Note: It is not possible to perform operations on number datatype and bigInt.

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

Often while writting programs we need to convert data from one form to another. Here are few conversion methods.
**1. Number to String**
 There are 3 ways to convert a number to string
  consider, const no=10;
1. String(no)
2. no.toString()
3. no+""
 
**2.String to Number**
There are 3 ways to convert a string to number
  consider, const str='10';
1.Number(str);
2.+str
3.parseInt(str)

**Common Methods on Number object**

```
Number.isInteger(3.5);            //false
Number.parseInt('5.8');           //5
Number.parseFloat('5.86');        //5.86
Number.isNaN(NaN);                //true
Number.isNaN('text');             //false
10.567.toFixed(2)                 //10.57
10.567.toPrecision('3');          //10.6
```

NaN
NaN is a special type of number which results by invalid mathematical operations. For eg subtracting a string and number.

```
Number.isNaN('text'-10)        //true
```
**Methods on Math object.**

```
Math.abs(-5);                 //5
Math.floor(1.6)               //1
Math.ceil(3.5)                //4
Math.round(3.67)              //4
Math.max(-5,3,8)              //8
Math.min(-5,3,8)              //-5
Math.sqrt(64)                 //8
Math.pow(5,3)                 //125
Math.trunc(-6.3)              //-6
```
**Symbol** 
Symbol represents  a unique identifier.Symbols are used to identify object properties and to add unique property keys to an object.

```
let symbol1=Symbol();
```