Data Types In JavaScript

martinwachira

Martin Wachira

Posted on January 27, 2023

Data Types In JavaScript

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

let age = 18;                          
let num = 18;                          

let isEqual = age === num;            
console.log(isEqual); //Output -> true
Enter fullscreen mode Exit fullscreen mode

Non-primitive data types are compared by reference, that is, two objects are strictly equal if they refer to the same values.


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

Enter fullscreen mode Exit fullscreen mode

Primitive Data types

Primitive data types are those that hold a single value.


let x = 5
let y = 12.5
Enter fullscreen mode Exit fullscreen mode

They include:

1. Number

It can be an integer, a float , an exponential, a NaN and an infinity.


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

Enter fullscreen mode Exit fullscreen mode

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.


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

Enter fullscreen mode Exit fullscreen mode

Infinity, it results when we divide a number by zero


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

Enter fullscreen mode Exit fullscreen mode

2.Boolean

Holds a true or false value.
It is mostly used to check if a logical condition is true or false.


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

Enter fullscreen mode Exit fullscreen mode

3.Null

Is a special value that represents an unknown value.


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

Enter fullscreen mode Exit fullscreen mode

4.Undefined

It represents a value that is not defined.


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

Enter fullscreen mode Exit fullscreen mode

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.


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

Enter fullscreen mode Exit fullscreen mode

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


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

Enter fullscreen mode Exit fullscreen mode

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.


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.

Enter fullscreen mode Exit fullscreen mode

How to access data in objects

To access values in an object, we use the dot notation and bracket notation.

Dot notation


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

Enter fullscreen mode Exit fullscreen mode

Bracket Notation


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

Enter fullscreen mode Exit fullscreen mode

Adding a value in an object


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

Enter fullscreen mode Exit fullscreen mode

Delete a value in the object


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

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

Enter fullscreen mode Exit fullscreen mode

Modifying value of a property in an object


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' 
  }

Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
martinwachira
Martin Wachira

Posted on January 27, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

JavaScript Variables
javascript JavaScript Variables

March 2, 2023

Data Types In JavaScript
javascript Data Types In JavaScript

January 27, 2023

Why are the results like this?
javascript Why are the results like this?

December 27, 2022