JavaScript Data Types
Quick Guide to Primitives and Objects
Primitive Data Types πͺ¨
JavaScript offers a set of fundamental data types, known as primitive types, that represent basic data values. These include:
Number π’
Represents both integers and floating-point numbers. Examples: 42
, 3.14
, -0.5
. Special values include Infinity
, -Infinity
, and NaN
(Not-a-Number).
String π€
Used for text. Strings can be enclosed in single quotes, double quotes, or backticks. Examples: 'hello'
, "world"
, `Hello, ${name}!`
.
Boolean π¦
Represents logical values: true
or false
. Example: let isActive = true;
.
Undefined β¨
A declared variable without an assigned value. Example: let x; // x is undefined
.
Null π³οΈ
Represents the intentional absence of any value. Example: let y = null;
.
Symbol π£
Unique and immutable values are often used as object keys. Example: let sym = Symbol('description');
.
BigInt βοΈ
For integers beyond the safe integer range of Number type. Example: let bigInt = 1234567890123456789012345678901234567890n;
.
Non-Primitive Data Types (Objects) π₯
While primitive types handle basic data, JavaScript also provides more complex data structures to organize information:
Object π
Stores collections of data and more complex entities. Example: let person = { name: 'John', age: 30 };
.
Array ποΈπππ
An ordered collection of values. Example: let arr = [1, 2, 3, 4];
.
Function π
A block of code for performing tasks. Example: function greet() { return 'Hello'; }
.
Date π
Handles dates and times. Example: let now = new Date();
.
RegExp π§¬
Pattern matching within strings. Example: let regex = /ab+c/;
.
Map and Set πΊοΈ
Map
holds key-value pairs; Set
holds unique values. Examples: let map = new Map();
, let set = new Set();
.