Data Types In JavaScript
Tags: javascript, beginners, programming, codenewbie
Published: 2023-01-27 12:20:38

A data type, is a classification that specifies which type of value a variable has and what type of mathematical, relational or logical operations can be applied to it without causing an error.
Basic data types in JavaScript include:
1.	Number
2.	String
3.	Boolean
4.	Symbol
5.	Null
6.	Undefined
7.	Object

The above data types are categorized into two categories:
1.Primitive data types, these are data types that store a single value. They include number, String, Boolean, Symbol, Null and Undefined.
2.Non primitive (Reference) data types, these data types are derived from primitive data types and have properties and methods to manipulate their values. An example is the object data type.

## Difference between Primitive and non-primitive Data types
1.Primitive data types are compared by value, that is, two values are strictly equal if they have the same value.
Sample

```JavaScript
let age = 18;                          
let num = 18;                          

let isEqual = age === num;            
console.log(isEqual); //Output -> true
```
Non-primitive data types are compared by reference, that is, two objects are strictly equal if they refer to the same values.

```JavaScript

let sports1 = ['Football', 'Rugby', 'Basketball'];                          
let sports2 = sports1;                                                                                     
let isEqual = sports1 === sports2;                
console.log(isEqual); //Output -> true

```
###Primitive Data types
Primitive data types are those that hold a single value.
```JavaScript

let x = 5
let y = 12.5
```
They include:
###1. Number
 It can be an integer, a float , an exponential, a NaN and an infinity.
```JavaScript

let x = 100; //An integer     
let y = 12.43; //A float      
let z = 10e4; //An exponential

```
NaN(Not a Number) is used to represent an invalid number, results when you try to perform an operation on a number with a non-numeric value.
```JavaScript

let b = 12 * 'f';
console.log(b); //Output -> NaN

```
Infinity, it results when we divide a number by zero
```JavaScript

let b = 12 / 0;                      
console.log(b); //Output -> Infinity

```
###2.Boolean
Holds a true or false value.
It is mostly used to check if a logical condition is true or false.
```JavaScript

let isGreater = 3 < 9;                                   
console.log(isGreater); //Output -> true

```
###3.Null
Is a special value that represents an unknown value.
```JavaScript

let b = null;                   
console.log(b); //Output -> null

```

###4.Undefined
It represents a value that is not defined.
 ```JavaScript

let b;                               
console.log(b); //Output -> undefined

```
###5.String
A string is any group of characters surrounded by quotes, double quotes or backticks.
When using double quotes or single quotes, there is no practical difference between them.
```JavaScript

let country = "Kenya";
let city = 'Nairobi';

 ```
A backtick is mostly used when appending a variable to a string
```JavaScript

let backtick = 'backtick';                                          
console.log (`This is a ${backtick}`);
//Output -> This is a backtick

```
###Non-Primitive Data Types
###1.Objects
In JavaScript, an object is a collection of properties where each property is defined with a key-value pair.
```JavaScript

let obj = {
country: 'Kenya',
capital: 'Nairobi',    
continent: 'Africa'
}    
The above object has three properties country, capital and continent with corresponding values of Kenya, Nairobi and Africa.

```
###How to access data in objects
To access values in an object, we use the dot notation and bracket notation.
###Dot notation 	
```JavaScript

objectName.propertyName //Syntax to access values of an object    

let countryName = obj.country;              
console.log(countryName); //Output -> Kenya  
let capitalName = obj.capital;               
console.log(capitalName); //Output -> Nairobi

```
###Bracket Notation
```JavaScript

objectName['propertyName'] //Syntax to access values of an object    
let countryName = obj['country'];              
console.log(countryName); //Output -> Kenya  
let capitalName = obj['capital'];               
console.log(capitalName); //Output -> Nairobi

```
###Adding a value in an object
```JavaScript

obj.population = '51.09 million';
console.log(obj);
// Output
{
 country: 'Kenya',
 capital: 'Nairobi',
 continent: 'Africa',
 population: '51.09 million'
 }
 
```

###Delete a value in the object
```JavaScript

delete obj.population;
console.log(obj);

//Output
{
 country: 'Kenya',
 capital: 'Nairobi',
 continent: 'Africa',
}

```
###Modifying value of a property in an object
  ```JavaScript

let intialObj = 
{
country: 'Kenya',
capital: 'Nairobi',
continent: 'Africa',
population: '51.09 million'
}

intialObj.country = 'Tanzania';//Changes the value Kenya to Tanzania
console.log(intialObj);
//Output 
{
 country : 'Tanzania', 
 capital : 'Nairobi', 
 continent :'Africa',
 population : '51.09 million' 
  }

```