13 Feb 2024
JavaScript has several data types, which can be categorized into two main categories: primitive data types and non-primitive (reference) data types.
- Primitive Data Types:
Primitive data types are immutable (i.e., once they are created, their values cannot be changed) and hold a single value. They are directly stored in memory and accessed by their value. Primitive data types are passed by value.
- Number: Represents numeric values, including integers and floating-point numbers.
- Range of the
numberdata type:- Minimum Value:
-Number.MAX_VALUE(-1.7976931348623157e+308) - Maximum Value:
Number.MAX_VALUE(1.7976931348623157e+308) - Integer Range:
-2^53 + 1to2^53 - 1(-9007199254740991 to 9007199254740991) - Floating-Point Precision: Approximately 15 decimal digits of precision
- Minimum Value:
- JavaScript has special numeric values like
Infinity(positive infinity),-Infinity(negative infinity), andNaN(Not-a-Number). which are part of thenumberdata type but do not fall within the traditional numeric range.
- Range of the
let num = 10;- String: Represents textual data, enclosed within single quotes (''), double quotes ("") or backticks (``).
let str = "Hello, world!";- Boolean: Represents a logical value, which can be either
trueorfalse.
let isTrue = true;- Undefined: Represents a variable that has been declared but not assigned a value, or a function that returns no value.
let undefinedVar;- Null: Represents the intentional absence of any object value.
let nullVar = null;- Symbol: Introduced in ECMAScript 6, represents a unique identifier.
const symbol = Symbol('description');- BigInt: Introduced in ECMAScript 2020, represents integers with arbitrary precision.
const bigIntNum = 9007199254740991n; - Number: Represents numeric values, including integers and floating-point numbers.
💡Note: Primitive data types (like numbers, strings, and booleans) are immutable. Once they are created, their values cannot be changed. For example, if you have a string
"hello", you cannot change it to"world". Instead, you create a new string with the desired value.
⭐Note: Remember, each time you "change" the value of a primitive data type, you are actually creating a new value and assigning it to the variable. This is because primitive data types are immutable in JavaScript.
- Non-primitive (Reference) Data Types:
Non-primitive data types are mutable (i.e., once they are created, you can modify the properties and elements of these objects directly) and hold references to values, rather than the values themselves. They are stored as references in memory. Non-primitive data types are passed by reference.
- Object: Represents a collection of key-value pairs. Objects can contain primitive data types, other objects, and functions.
let person = { name: 'John', age: 30 };- Array: A special type of object used to store multiple values in a single variable.
let numbers = [1, 2, 3, 4, 5];- Function: A callable object that executes a block of code.
function greet(name) { console.log(`Hello, ${name}!`); }- Date: Represents a date and time value.
let currentDate = new Date();- RegExp: Represents a regular expression object used for pattern matching within strings.
let pattern = /[a-zA-Z]+/;- Map, Set, WeakMap, WeakSet: Introduced in ECMAScript 6, these are specialized data structures used for storing collections of data.
let map = new Map(); map.set('key', 'value');let set = new Set(); set.add(1);
💡Note: Non-primitive data types (like objects and arrays) are mutable. You can modify the properties and elements of these objects directly. For example, if you have an object
personwith propertiesnameandage, you can change the value ofnameoragedirectly without creating a new object.