Introduction
Each programming language supports multiple data types. These data types play a crucial role in every programming language. Big programs are made by the collection of all these data types. Moreover, based on these data types, we are able to craft an effective program. Typically, these are almost the same in all programming languages, but in some languages, they might be a bit different. Today, we will explore all these data types in detail from the perspective of JavaScript.
These data types are String, Number, BigInt, Boolean, undefined, null, Symbol, Object, etc. They are distributed in two major categories: primitive and non-primitive.
Primitive data Types
In programming, primitive data types are basic data types that represent simple values. They are immutable (cannot be changed once created) and stored by value, meaning when you assign them to another variable, a copy of the value is made rather than a reference to the original value.
Non-Primitive Data Types
In programming, non-primitive data types (also known as reference types) are types that store references (or pointers) to the memory location where the actual data is stored, rather than storing the data itself. These types are typically more complex and allow for more advanced operations compared to primitive data types, such as working with collections of values, objects, and custom types. Unlike primitive data types, They are mutable meaning you can modify their properties and they are stored by reference.
Data Types In JavaScript
JavaScript is the most popular programming language; it supports multiple data types like other programming languages. It supports String, Number, BigInt, Boolean, undefined, null, Symbol, and Object. Like other programming languages, these data types are distributed in the category of two major data types that are primitive and non-primitive data types.
Primitive data type
- String
- Number
- BigInt
- Boolean
- Undefined
- Null
- Symbol
String: In JavaScript, a string is a primitive data type used to represent a sequence of characters. Strings are used for handling and manipulating text. A String is used for storing text by using quotes ('') It can be created in both single and double quotations as you can see in the example below:
let str1 = 'this is string data type';
let str2 = "this is string data type";
console.log(typeof str1) // output: string
console.log(typeof str2) // output: string
As you can see, a string can be created in both single ('') and double ("") quotations. And typeof is a built-in method in JavaScript for checking the data type of any variable.
Number: In JavaScript, a Number is a primitive data type used to store numeric values like integers (a numeric value without decimal places like 12) and floating points (a numeric value with decimal places like 2.5). Unlike a String in a Number, a numeric value is stored without quotes.
let num1 = 123;
let num2 = 2.5;
console.log(typeof num1); // output: number
console.log(typeof num2); // output: number
BigInt: JavaScript BigInt variables are used to store large integer values that are too big to be represented by a normal JavaScript Number. A BigInt is created by appending the n suffix to the end of a number.
In JavaScript, the largest integer that can be safely represented by a Number is 253 - 1 (which is 9,007,199,254,740,991). Any number larger than this may lose precision. However, BigInt can handle arbitrarily large integers.
// Normal JavaScript Number (max safe integer is 2^53 - 1)
let maxSafeNumber = 9007199254740991; // 2^53 - 1
console.log(maxSafeNumber); // Output: 9007199254740991
// BigInt to handle larger numbers
let bigIntValue = 9007199254740992n; // BigInt notation with 'n' at the end
console.log(bigIntValue); // Output: 9007199254740992n
console.log(typeof maxSafeNumber) // output: number
console.log(typeof bigIntValue) // output: bigint
Boolean: In JavaScript, the Boolean data type represents one of two possible values: true or false. These values are commonly used in conditions and control flow statements (like if/else, while, etc.) to control the behavior of a program based on logical decisions.
let active = true;
let notActive = false;
console.log(typeof active); // output: boolean;
console.log(typeof notActive); // output: boolean;
Undefined: In JavaScript, Undefined is a primitive data type it is a special default value in case any variable is created and not assigned any value in that variable, then JavaScript assigns a default value that is Undefined.
let myCar;
console.log(typeof myCar) // output: undefined;
As you can see, we declared a variable myCar without assigning any value, it is returning undefined.
Null: In JavaScript, null is a primitive data type that represents the intentional absence of any object value. It is used to indicate that a variable has been explicitly assigned to have no value or that it is empty
let myCar = null
console.log(typeof myCar); // output: object;
In JavaScript, the typeof operator is a bit misleading when examining the null data type. It returns object instead of null. This is considered a historical bug in JavaScript that has been kept for backward compatibility. There is also some confusion surrounding the null data type in JavaScript due to this behavior.
Symbol: In JavaScript, a Symbol is a primitive data type It is used to create unique, immutable identifiers that can be used as property keys for objects. Each Symbol is unique even if the description or value is the same. As you can see in the example below:
let symbol1 = Symbol('description');
let symbol2 = Symbol('description');
console.log(symbol1 === symbol2); // Output: false (because they are unique)
console.log(typeof symbol1) // output: symbol
console.log(typeof symbol2) // output: symbol
We covered all the primitive data types from the perspective of JavaScript. Now we will explore the non-primitive data types in JavaScript.
Non-Primitive data type
In JavaScript, objects and arrays come in the Non-primitive data type category, like in most programming languages.
Object: An object is a non-primitive data type that is used to store multiple data types in one container; each element in the object is created by a property and its value. It can save multiple data types (like both primitive and non-primitive data types) within it. In modern programming, it is widely used in every programming language. You can see what an object looks like:
let person = { name: "John", age: 30, male: true };
console.log(typeof person) // object
As you can see in the example above, we created an object within another object; each element is defined by a property, and a value is assigned to its corresponding property. Additionally, this object handles three types of data: string (name: 'John'), number (age: 30), and boolean (male: true).
Array: Like object array is also a type of non-primitive data type, it is also used for storing multiple data types in one container but here each element is stored based on their index number. Arrays are the widely used data types in all programming languages for storing multiple values in one container. Like an object, it is known by the same identity as the object, and you can see in the example below:
let arr = ['apple', 'water', 33, true]
console.log(typeof arr) // object
As you can see, we created an array within the array We saved three types of data type String, Number, and Boolean, and in the array, each element is identified by its index number.