# DATA TYPES in JavaScript

To be able to operate on variables, it is important to know something about the type.

* Primitive types
    
* Abstract operations
    
* Coercion
    
* Equality
    
* TypeScript
    
* Flow
    

**<mark>Primitive types:</mark>** The predefined data types provided by JavaScript language are known as primitive data types. Primitive data types are also known as in-built data types.  
Primitive values are immutable and are compared by value.

1\. String  
2\. Number  
3\. Bigint-available from ES2020  
4\. Boolean  
5\. Undefined  
6\. Null  
7\. Symbol-available from ES2015  
8\. Object

***The undefined type:*** is a primitive type that has only one value undefined. By default, when a variable is declared but not initialized, it is assigned the value of undefined.  
Non-primitive values are mutable compared by reference, not value

For example:

```javascript
let subject;
console.log(subject);        // undefined
console.log(typeof subject); // undefined
```

***The null type:*** The null type is a primitive data type that also has only one value null. For example:

```javascript
let obj = null;
console.log(typeof obj); // object
```

**<mark>Non-primitive data types:</mark>** The data types that are derived from primitive data types of the JavaScript language are known as non-primitive data types. It is also known as derived data types or reference data types.

an example is Object and the the data structures considered as objects.

**typeof Operator** The type of operator returns a string indicating the type of the operand's value

```javascript
console.log(typeof 42);
// Expected output: "number"

console.log(typeof 'blubber');
// Expected output: "string"

console.log(typeof true);
// Expected output: "boolean"

console.log(typeof undeclaredVariable);
// Expected output: "undefined"
```

***<mark>Kinds of emptiness</mark>:*** Emptiness in JavaScript is not as clear cut as in more strictly typed languages, but we can probably agree on some if not most of the following criteria:

* undefined or null
    
* undeclared
    
* uninitialised (aka TDZ)
    

***<mark>NaN &amp; isNaN:</mark>***

NaN, in JavaScript, can be many things. In fact, it can be almost anything, so long as it is *Not a Number*. Its type is technically “number” (when evaluated with “typeof”), although it stands for **Not a Number**. Values can become NaN through a variety of means, which usually involve erroneous math calculations (such as 0/0), or as a result of type coercion, either implicit or explicit.

A common example is when you run parseInt on a string that starts with an alphabetical character. This isn’t exclusive to parseInt, as it also applies when using explicit coercion with Number(), or with the unary “+” operator.

NaN is a bizarre value in JavaScript, as it does not equal itself when compared, either with the loose equality (==) or strict equality (===) operator. NaN is the only value in the entire language which behaves in this manner with regards to comparisons.

\-&gt;JavaScript has a built-in method, appropriately named “isNaN,” which checks for NaN. There is a newer function called Number.isNaN, which is included in the ES2015 spec. so if a value is a NaN we get true otherwise flase

```javascript
var myAge = Number("0o46");          //18
var myNextAge = Number("19");        //19
var myCatsAge = Number("n/a");       //NaN (n/a-not applicable)
myAge - "my friends age";

myCatsAge === myCatsAge;             //false

isNaN(myAge);                        //false
isNaN(myCatsAge);                    //true
isNaN("my friends age");             //true

Number.isNaN(myCatsAge)              //true
Number.isNaN("my son's age");        //false   
```

in line 5 of our code it shows flase becuse javascript says two NaN values are never equal, NaN is the only value in JavaScript that doesnt have the 'identity property'.

//////

<mark>Negetive Zero:</mark>

Although we often don’t distinguish between -0 and 0 in everyday programming, it might be useful for rare cases. For example, a negative zero can be used to express a sense of direction (left or right, up or down). This could also be helpful in representing a moving object on a map (x and y coordinates).

The IEEE 754 standard for floating point arithmetic (presently used by most computers and programming languages that support floating point numbers) requires both +0 and −0.

```javascript
var trendRate = -0;
trendRate === -0;            //true

trendRate.toString();        //"0"
trendRate === 0;             //true   
trendRate < 0;               //false
trendRate > 0;               //false

Object.is(trendRate,-0);     //true
Object.is(trendRate,0);      //false;  
```

* The condition number &gt; 0 checks if the number is **positive**.
    
* The condition number == 0 checks if the number is **zero**.
    
* The condition number &lt; 0 checks if the number is **negative**.
    

```javascript
math.sign(-3)            //-1
math.sign(3)             //1
math.sign(-0)            //-0
math.sign(0)             //0      
```

```javascript
var trendRate = -0;
trendRate === -0;

trendRate.toString();
trendRate === 0;
trendRate < 0;
trendRate > 0;

Object.is(trendRate,-0);
Object.is(trendRate,0);
```

so to fix the mathsign we write a function where math.sign(-0) would give us -1 and not -0.

```javascript
function sign(v)  {
  return v !== 0 ? math.sign(v) : object.is(v,-0) ? -1 : 1;
}
```
