Data Types in JavaScript

Data Types in JavaScript

·

3 min read

Data Types in JS

JavaScript data types are the fundamental building blocks of any program. They represent the different types of information that can be stored and manipulated in the language. JavaScript has several data types, each with its own unique characteristics. We will examine fundamental data types and then delve into real-life examples to further explore them.

JavaScript has primitive and non-primitive data types:

Primitive Data Types

Number:

A number represents numeric values, including both integers and floating-point numbers.

let num1 = 30;     // Datatype is number or integer because declare
let num2 = 14.1;   // a numeric value
console.log(num1);
console.log(num2);

String:

A string represents sequences of characters enclosed in single quotes (' '), double quotes (" "), and backticks (` `).

let firstName = "Muhammad"; // Datatype is string
let lastName = 'Bilal'; // Datatype is string
console.log(firstName);
console.log(lastName);

Boolean:

A boolean represents logical values, either true or false.

let isWin = true; // Datatype is boolean, if it is true then print 1
                   // if false then print 0.
console.log(isWin);

Undefined:

Undefined represents the absence of a value or an uninitialized variable (the variable that has been declared but has not been assigned any value yet).

let myVal; // Datatype is undefined because we has not assigned
           // it any value
console.log(myVal);

Null:

Null represents the intentional absence of a value or an empty value.

// We didn't declare any value so there is null but we can assign 
// null value
let myNewVal = null; // This is also a null value.
console.log(myNewVal);

Symbol:

A symbol represents unique identifiers.

// We use symbol to represents the unique identifiers mostly in objects.
// They used in React.

BigInt:

Bigint represents integer values larger than the maximum limit of the number type.

// Mostly we didn't use Bigint we use number datatype but Number 
// has its limit and when we crossed that limit then to handle them 
// we use Bigint.

Non-Primitive Data Type

In JavaScript, non-primitive data types are complex data structures that store collections of data, functions, or other objects. They are represented by reference values, which means that they store a reference to the actual data structure in memory rather than the data itself. This distinguishes them from primitive data types, which store the actual value directly.

1. Objects:

In JavaScript, objects are the primary non-primitive data type. They consist of key-value pairs, with each key serving as a unique identifier and each value capable of being any data type, such as other objects, arrays, or functions. Objects are dynamic and mutable, allowing their properties and values to be modified after creation.

let myObj = {
    firstNmae: "Muhammad",
    lastName: "Bilal",
    age: 21,
    favFruits: ["Banana", "Apple"],
}
console.log(myObj.firstNmae);
console.log(myObj.lastName);
console.log(myObj.age);
console.log(myObj.favFruits);

2. Arrays:

Arrays are specific objects for holding an organized set of elements. Each element within an array can be of any data type, and arrays can vary in length. Arrays are effective for both storing and retrieving sequential data.

let newArr = ["Muhammad", "Bilal", 21, "Badminton"]; // Index start from 0
console.log(newArr[0]); // Print => Muhammad 
console.log(newArr[1]); // Print => Bilal 
console.log(newArr[2]); // Print => 21 
console.log(newArr[3]); // Print => Badminton

3. Functions:

Functions define a reusable set of instructions and can take input parameters, perform operations, and return a value. They are crucial for modularizing code and executing repetitive tasks.

function myFunc(){
    console.log("This is a Function");
}
myFunc();

4. Dates:

Dates are used to capture particular moments in time, holding details including year, month, day, hour, minute, second, and millisecond. They are valuable for handling time-related data and conducting date and duration calculations.

const date = new Date();
const d = new Date("2022-03-25");

We will discuss them in depth with examples in upcoming blogs.

Â