Data Types in JavaScript
JavaScript has two main data types: primitives and objects. Let's examine each of them.
Primitive Types
Primitives are basic data types. Their value is immutable, and they're passed by value.
List of Primitives
Number
String
Boolean
Null
Undefined
Symbol
BigInt
Important:
Primitives are immutable. For example, string methods don't change the string itself, but return a new one.
Objects
Objects are collections of data and functionality. They're passed by reference.
Examples of Objects
- Object — basic object.
- Array — ordered data collection.
- Function — object representing executable code.
- Date — object for working with dates.
- RegExp — object for working with regular expressions.
Examples of Creating Objects
// Regular object
const obj = { name: "John", age: 30 };
// Array
const arr = [1, 2, 3];
// Function
function greet() {
console.log("Hello!");
}
typeof Examples
// typeof examples
console.log(typeof obj); // "object"
console.log(typeof arr); // "object"
console.log(typeof greet); // "function"
console.log(typeof null); // "object" (JS peculiarity)
console.log(typeof undefined); // "undefined"
console.log(typeof 42); // "number"
console.log(typeof "Hello"); // "string"
console.log(typeof Symbol("id")); // "symbol"
console.log(typeof 123n); // "bigint"
typeof null Peculiarity:
To check data type use typeof. Remember that typeof null returns "object" — this is a historical bug in JavaScript.