JavaScript Data Types
Learn about JavaScript data types, understand the difference between primitive and non-primitive types, and see how they are used in real-world applications.
Reading Time
12 min
Lesson
Lesson 5 of 48
What are Data Types?
A data type defines the kind of value a variable can store. JavaScript supports different types of data such as numbers, strings, booleans, objects, and more. Understanding data types is essential because every operation in JavaScript depends on the type of data being used.
Why are Data Types Important?
Data types help JavaScript determine what operations can be performed on a value. For example, numbers can be added, while strings can be concatenated. Choosing the correct data type makes your programs more reliable and easier to understand.
JavaScript Data Types
- String
- Number
- Boolean
- Undefined
- Null
- BigInt
- Symbol
- Object
Primitive vs Non-Primitive
| Primitive | Non-Primitive |
|---|---|
| String | Object |
| Number | Array |
| Boolean | Function |
| Undefined | Date |
| Null | Map / Set |
| BigInt | Custom Objects |
| Symbol | - |
1. String
A string is a sequence of characters enclosed in single quotes, double quotes, or backticks. Strings are commonly used to store names, messages, and text.
String Example
Console Output
Click “Run” to see the console output here.
2. Number
The Number type represents both integers and decimal values. JavaScript does not have separate types for int and float.
Number Example
Console Output
Click “Run” to see the console output here.
3. Boolean
A Boolean represents one of two values: true or false. It is mainly used in conditions and decision-making.
Boolean Example
Console Output
Click “Run” to see the console output here.
4. Undefined
A variable that has been declared but not assigned a value has the value undefined.
Undefined Example
Console Output
Click “Run” to see the console output here.
5. Null
Null represents the intentional absence of a value. It is assigned explicitly by the developer.
Null Example
Console Output
Click “Run” to see the console output here.
6. BigInt
BigInt is used for very large integers that cannot be represented safely by the Number type.
BigInt Example
Console Output
Click “Run” to see the console output here.
7. Symbol
A Symbol is a unique and immutable value. It is commonly used as object property keys to avoid conflicts.
Symbol Example
Console Output
Click “Run” to see the console output here.
8. Object
Objects are collections of key-value pairs. Arrays, functions, and dates are all special types of objects in JavaScript.
Object Example
Console Output
Click “Run” to see the console output here.
typeof Operator
| Value | Result |
|---|---|
| "Hello" | "string" |
| 100 | "number" |
| true | "boolean" |
| undefined | "undefined" |
| null | "object" (historical bug) |
| {} | "object" |
| [] | "object" |
| Symbol() | "symbol" |
Using typeof
Console Output
Click “Run” to see the console output here.
Common Interview Question
Many developers expect typeof null to return "null". However, it returns "object". This is a well-known historical bug in JavaScript and is frequently asked in interviews.
Best Practice
Choose the correct data type for the data you want to store. Use null intentionally when a value is absent, and avoid relying only on typeof for complex type checking.
Summary
JavaScript provides seven primitive data types and one non-primitive type (Object). Understanding data types is essential because every variable, function, and expression in JavaScript works with them.