JavaScript is a dynamically typed language, meaning variables can hold different types of values at different times. Understanding JavaScript data types is fundamental for writing effective code. JavaScript provides several built-in data types, categorized into primitive types and objects.
TL;DR
The following table below provides a summary of the different JavaScript data types, their descriptions, and examples.
Data Type | Description | Example |
---|---|---|
String | Represents textual data | let name = "John"; |
Number | Represents numeric values (both integer and floating-point) | let age = 30; |
Boolean | Represents logical values: true or false | let isActive = true; |
Null | Represents an intentional absence of any object value | let emptyValue = null; |
Undefined | Indicates that a variable has not been assigned a value | let value; |
Symbol | Represents a unique and immutable value | let symbol = Symbol('description'); |
BigInt | Represents integers with arbitrary precision | let bigIntValue = 1234567890123456789012345678901234567890n; |
Object | Represents collections of properties | let person = { name: 'John', age: 30 }; |
Array | Represents ordered collections of values | let numbers = [1, 2, 3, 4, 5]; |
Function | Represents reusable blocks of code | function greet() { return "Hello"; } |
JavaScript data types are categorized into two main types: primitive and non-primitive (also known as reference) types.
Primitive data types include String, Number, Boolean, Null, Undefined, Symbol, and BigInt. Non-primitive data types include Object, Array, and Function.
Primitive Data Types
Primitive data types are the most basic data types in JavaScript. They include:
1. String
Strings represent text and are created using single quotes, double quotes, or backticks for template literals.
2. Number
Numbers represent both integer and floating-point values. Integers are whole numbers, while floating-point numbers have decimals.
3. Boolean
Booleans represent logical values: true or false.
4. Null
Null represents an intentional absence of any object value.
5. Undefined
Undefined means a variable has been declared but not assigned a value.
6. Symbol
Symbols are unique and immutable values, often used to identify object properties uniquely.
7. BigInt
BigInt is used for integers that are too large to be represented by the Number type.
Object Data Types
Objects are collections of properties, and they include more complex data structures such as arrays and functions.
1. Object
Objects are collections of key-value pairs.
2. Array
Arrays are ordered collections of values.
3. Function
Functions are reusable blocks of code.
Conclusion
Understanding JavaScript data types is crucial for writing effective and bug-free code. JavaScript provides a rich set of data types, including primitive types like strings and numbers, as well as objects, arrays, and functions. Mastering these data types will help you manipulate and utilize data effectively in your applications.