What are the primitive data types in JavaScript?
Table of Contents
Introduction
Primitive data types in JavaScript are the most basic types of data that represent simple values. They are immutable, meaning their values cannot be altered once created. JavaScript has seven primitive data types, and understanding them is crucial for managing data effectively.
Primitive Data Types in JavaScript
1. String
A string represents text. It is a sequence of characters enclosed in quotes (single or double).
Example:
2. Number
The number
type is used for both integers and floating-point numbers.
Example:
3. Boolean
A boolean has only two values: true
or false
. It is often used in conditional statements.
Example:
4. Undefined
A variable that has been declared but not assigned a value is of the type undefined
.
Example:
5. Null
null
is an intentional absence of any object value. It represents "nothing" or "empty."
Example:
6. Symbol
Introduced in ES6, symbol
is a unique and immutable data type often used as object property keys.
Example:
7. BigInt
The bigint
type allows for representation of whole numbers larger than the number
type can safely handle. It was introduced in ES2020.
Example:
Characteristics of Primitive Data Types
- Immutability: Once a primitive value is created, it cannot be modified.
- Stored by Value: Primitive types are directly stored in the location where the variable is accessed, not as a reference.
Conclusion
JavaScript's primitive data types — string
, number
, boolean
, undefined
, null
, symbol
, and bigint
— are fundamental building blocks for working with data. Each has specific uses and characteristics, making them essential for various programming scenarios. Understanding these types helps ensure proper handling and manipulation of values in JavaScript.