Javascript Data Types
JavaScript has several built-in data types that are used to represent different kinds of values. These data types can be categorized into two main categories: primitive data types and reference data types.
- Primitive Data Types: Primitive data types are the most basic data types in JavaScript. They are immutable (cannot be changed) and are stored directly in memory. The following are the primitive data types in JavaScript:
a. Number: Represents both integer and floating-point numbers. Example:
let num = 42;
b. String: Represents a sequence of characters. Example:
let str = "Hello, World";
c. Boolean: Represents a true or false value. Example:
let isTrue = true;
d. Undefined: Represents a variable that has been declared but not assigned a value. Example:
let undefinedVar;
e. Null: Represents an intentional absence of any object value. Example:
let nullVar = null;
f. Symbol (ES6): Represents a unique and immutable value, often used as object property keys. Example:
const sym = Symbol("unique");
g. BigInt (ES11): Represents large integers that cannot be represented by the Number data type. Example:
const bigIntValue = 1234567890123456789012345678901234567890n;
- Reference Data Types: Reference data types are more complex and store references to objects rather than the actual values. They are mutable, meaning their content can be changed. The following are common reference data types in JavaScript:
a. Object: Represents a collection of key-value pairs, where keys are strings (or Symbols) and values can be of any data type. Example:
const person = { name: "John", age: 30 };
b. Array: A specialized type of object that stores ordered collections of values, typically indexed by numbers. Example:
const numbers = [1, 2, 3, 4];
c. Function: A callable object that can be defined using function expressions or function declarations. Functions are used to perform actions and return values. Example:
function add(a, b) { return a + b; }
d. Date: Represents dates and times. It provides methods for working with dates and times. Example:
const currentDate = new Date();
e. RegExp: Represents regular expressions for pattern matching. Example:
const pattern = /abc/g;
f. Other objects (e.g., Map, Set, WeakMap, WeakSet): JavaScript has additional built-in objects that are used for specific purposes, like data structures (Map, Set) or handling weak references (WeakMap, WeakSet).
Understanding these data types and how they behave is essential for effective JavaScript programming. You can use operators and methods that are specific to each data type for various operations in your code.