In JavaScript, primitive types are the basic building blocks of the language. They represent single values and are immutable. Here are the six primary primitive types:
1. String
- Represents textual data.
- Enclosed in single quotes (
'
), double quotes ("
), or backticks (`
). - Example:
let name = "John";
2. Number
- Represents both integer and floating-point numbers.
- Example:
let age = 25; let price = 99.99;
3. Boolean
- Represents a logical entity with two values:
true
orfalse
. - Example:
let isAvailable = true;
4. Undefined
- A variable that has been declared but not assigned a value.
- Example:
let x; console.log(x); // undefined
5. Null
- Represents the intentional absence of any object value.
- Example:
let y = null;
6. Symbol
- Represents a unique and immutable identifier.
- Example:
let sym = Symbol('unique');
7. BigInt
- Represents numbers larger than the
Number
type can safely handle. - Example:
let bigIntValue = 1234567890123456789012345678901234567890n;
Characteristics of Primitive Types:
- Immutable: Their values cannot be changed once created. Operations on primitive values return new values.
- Stored by Value: When assigned or passed, they are copied.
Understanding these primitive types is crucial as they form the foundation for working with data in JavaScript.