Data Types in JavaScript
JavaScript Data Types
JavaScript has two main types of data:
Primitive and
Non-Primitive (Reference) types.
1. Primitive Data Types
These types store a single value and are immutable.
a) String
A sequence of characters enclosed in quotes.
let name = "John Doe";
console.log(typeof name); // Output: string
b) Number
Represents both integer and floating-point numbers.
let age = 25;
console.log(typeof age); // Output: number
c) Boolean
Represents true or false values.
let isLoggedIn = true;
console.log(typeof isLoggedIn); // Output: boolean
d) Undefined
A variable that has been declared but not assigned a value.
let x;
console.log(typeof x); // Output: undefined
e) Null
Represents an intentional absence of value.
let y = null;
console.log(typeof y); // Output: object (JavaScript quirk)
f) Symbol (ES6)
Used for unique identifiers.
let sym = Symbol("unique");
console.log(typeof sym); // Output: symbol
g) BigInt (ES11)
Used for very large numbers.
let bigNumber = 9007199254740991n;
console.log(typeof bigNumber); // Output: bigint
2. Non-Primitive (Reference) Data Types
These types store references to memory locations.
a) Object
Used to store key-value pairs.
let person = { name: "Alice", age: 30 };
console.log(typeof person); // Output: object
b) Array
A collection of values in a single variable.
let fruits = ["Apple", "Banana", "Cherry"];
console.log(typeof fruits); // Output: object
c) Function
A reusable block of code.
function greet() {
return "Hello!";
}
console.log(typeof greet); // Output: function
d) Date
Represents date and time.
let today = new Date();
console.log(typeof today); // Output: object
Conclusion
JavaScript has **7 primitive** and **several non-primitive** data types.
Primitive types store values directly, while non-primitive types store references.