Primitive type
Tags: javascript, 33jsconcepts, webdev, jsnotes
Published: 2022-04-02 17:15:41

***javaScript*** Oh boi!! the defacto language for Web. And for today we will be focusing on it's types mainly its ___Primitive type___ .

But before jumping on Primitive type, lets discuss bit about the language itself. Javascript is a dynamic language it basically means variables in javascript are not directly associated with any particular type.

###Depending on its type  js  broadly can be divided upon ::
 1. Primitive types.
 2. Objects.

## Primitive Type
 **Primitive Type** is immutable by nature, So what does it means,
a piece of code can summarize it nicely.
```javascript
let number1 = 1;// we are declaring a variable and assigning with a Number 1
number1 = 2;//Here we are reassigning number1 to 2, in any way we are not updating it.
```
So, As in the comments in the above code we are not mutating the variable simply we are reassigning it. As for every other Primitive type they are immutable in javascript.

### In JS we have 7 primitive types ::
1.Number.
2.Big Int.
3.String.
4.Undefined.
5.null(techically object)
6.Boolean
7.Symbol(recent addition the langauge ES2015)

But, we are going too fast right, some people might ask how do get to know its type . Simply we can use `typeof` operator to check for it, it will be useful so we will try to remember it.

####Numeric Type ::
   Javascript has two Numeric types
   1.Number type
   2.BigInt
 
 **Number type** in javascript are not integers they are floating- 
 point numbers, Technically _double-precision 64-bit binary 
 format_. So below is Code Summary
```javascript
const num = 1;//variable assignment to a Number type
typeof(num); //will return type of number
console.log(Number.MAX_SAFE_INTEGER);//will return `9007199254740991` as the max safe integer.
console.log(Number.MIN_SAFE_INTEGER);//will return `-9007199254740991` as the min safe integer.
```
**BIG INT** comes handy if you want to work on really big numbers even beyond `Number.MAX_SAFE_INTEGER` or its inverse `Number.MIN_SAFE_INTEGER` . [Mozilla docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#primitive_values) wraps it nicely have a look if you want to dive deeply. we will skip to next type😎.

####String Type ::
**String** is fundamental type regardless of whatever type of language it is. Rule of Immutability play's here as well it means when we are adding a character or concatenating two strings we are reassigning the previous string to a new string, not necessarily modifying the old string.
Its fairly a basic data type for any language. So, look at the code below ::
```javascript
const name = "Richard"//A simple variable declarations with a string assignment
typeof(name)/will return string
```
####Undefined
Yes **undefined** is a type  and it is used  implicitely in javascript even if your not using it explicitly. what does it means below code block it clear it out.
```javascript
let name //say you forgot to assign value to the value, javascript implicitely assigns undefined as its value.
typeof(name);//so it will return undefined as its value.
//avoid undefined in case of intentional empty variable assignment use null instead.
```
####null type
**null** is a primitive type in javascript, it is represented by literal `null`. and mostly null represents intentional absence of any value by developer.
But `typeof null` returns objects type there are many article are out there for this weird behaviour, its a bug to-be precise according to lot of articles.
```javascript
let name = null;//intentional absence of any value assignment.
typeof(name); //will return object , weird isn't it.
//both undefined & null seems to be same but they are not
console.log(undefined == null);// will return true they are both absent of any value.
console.log(undefined === null);// will return false here cause we are checking both value and type. Their type is different
```
above code also makes it clear that `undefined` and `null` are not same they have some similarity but of different use. be smart before using each of one them.

####Boolean type
Boolean types are represented by two literal `true` and `false`, yes as simple as that. And Booleans are everwhere from any conditional like if-else,switch to operator like `==` & `===`. So proper  understading is mandatory. Code below will clear it out.
```javascript
const bool = true // will assign true boolean as value to bool variable.
typeof(boo);//will return boolean
if(bool){return "boole= "+bool}// here if block is going to run only if the value bool evalutes to true try changing to false it will not run
```
####Symbol
Symbol is a primitive type in javascript like other types mentioned above. Its Added in ECMAScript 2015 fairly recent addition. unlike others Number,String,null,undefined,boolean it **doesn't have any literal** it represent it

To create a new Symbol we use global method method like below ::
```javascript
let userName = Symbol('name')
```
As with Symbol with each invokation of `Symbol() `it returns unique private value so `Symbol() == Symbol()` will be false.
```javascript
Symbol() == Symbol();//returns false
```

So, here we are at the end of this post. I made an effort to simplyfy things in javascript which seems daunting from a new developers perspective. javascript have lot of concepts which are needed to-be well understood to leverage the language itself, I am following this [**amazing github repository**](https://github.com/leonardomso/33-js-concepts) for holistic javascript undersanding, Check it guys/gals.