Js Data Types in JavaScript
? JavaScript Data Types
JavaScript has 7 primary data types, divided into primitive and non-primitive:
1. Primitive Data Types
| Type | Description | Example |
|---|---|---|
| Number | Represents both integers and floating points | 42, 3.14, NaN |
| String | Sequence of characters | "Hello", 'JS' |
| Boolean | Logical true/false values | true, false |
| Undefined | Variable declared but not assigned | let x; // x is undefined |
| Null | Represents intentional absence of value | null |
| BigInt | For very large integers beyond Number range | 9007199254740991n |
| Symbol | Unique and immutable identifier | Symbol('id') |
2. Non-Primitive Data Type (Objects)
Objects are collections of key-value pairs.
Arrays, functions, dates, etc. are specialized objects.
const obj = {name: "Alice", age: 25};const arr = [1, 2, 3];Examples:
typeof 42; // "number"typeof "hello"; // "string"typeof true; // "boolean"typeof undefined; // "undefined"typeof null; // "object" (this is a quirk in JS)typeof 10n; // "bigint"typeof Symbol('id'); // "symbol"typeof {}; // "object"typeof []; // "object"typeof function(){}; // "function"Notes:
nullis a primitive buttypeof nullreturns"object"(historical bug).functionis a special callable object.JavaScript is dynamically typed, so variables can hold any data type.
Want me to explain type coercion, checking types, or how to convert between types?