JavaScript Data Types
This lesson explains JavaScript Data Types in JavaScript with beginner-friendly examples, practical use cases, and clear best practices.
JavaScript Data Types Overview
JavaScript data types define the kind of values that variables can store.
Every value in JavaScript belongs to a specific data type, allowing the
JavaScript engine to determine how the value should be stored, compared,
and manipulated during program execution.
JavaScript is a dynamically typed language, meaning variables can hold
different data types throughout their lifetime without requiring explicit
type declarations. Modern JavaScript supports eight built-in data types,
including primitive and reference types.
| Category | Data Type | Example |
| Primitive | String | "Hello" |
| Primitive | Number | 100 |
| Primitive | Boolean | true |
| Primitive | Undefined | undefined |
| Primitive | Null | null |
| Primitive | BigInt | 9007199254740991n |
| Primitive | Symbol | Symbol() |
| Reference | Object | Object, Array, Function, Date |
JavaScript Data Types Example
const name = "John";
const age = 30;
const isDeveloper = true;
let city;
const salary = null;
const largeNumber = 9007199254740991n;
const uniqueId = Symbol();
const person = {
firstName: "Subrahmanyam",
country: "USA"
};
console.log(typeof name);
console.log(typeof age);
console.log(typeof isDeveloper);
console.log(typeof city);
console.log(typeof person);
This example demonstrates several JavaScript data types including strings,
numbers, booleans, undefined values, null values, BigInt, Symbol, and
objects.
JavaScript Data Types with console.log Output
| Data Type | Description | console.log Example | Output |
| String | Stores text values. | console.log(typeof "Hello") | string |
| Number | Stores integers and decimals. | console.log(typeof 100) | number |
| Boolean | Stores true or false. | console.log(typeof true) | boolean |
| Undefined | Variable declared but not assigned. | let city; console.log(typeof city) | undefined |
| Null | Represents an intentional empty value. | console.log(typeof null) | object (legacy behavior) |
| BigInt | Stores very large integers. | console.log(typeof 9007199254740991n) | bigint |
| Symbol | Creates unique identifiers. | console.log(typeof Symbol("id")) | symbol |
| Object | Stores collections of values. | console.log(typeof { name: "Sam" }) | object |
Best Practices
- Use the correct data type for the information being stored.
- Use
const whenever possible. - Avoid unnecessary type conversions.
- Use strict equality operators when comparing values.
- Check data types using
typeof when debugging. - Use objects for related data instead of multiple standalone variables.
- Use arrays to store ordered collections of similar values.
- Validate data received from APIs before processing.
Common Mistakes
- Confusing
null with undefined. - Assuming arrays are separate data types instead of objects.
- Comparing values with loose equality.
- Ignoring automatic type conversion.
- Using numbers for very large integers instead of BigInt.
- Not checking data types before calculations.
- Forgetting that
typeof null returns "object".
Key Takeaways
- JavaScript has eight built-in data types.
- Primitive data types store single immutable values.
- Objects are reference data types that store collections of values.
- JavaScript is dynamically typed.
- The
typeof operator helps identify most data types. - Choosing the correct data type improves performance and code quality.
Pro Tip
Understanding JavaScript data types is essential before learning objects,
arrays, functions, JSON, APIs, and asynchronous programming. Strong
knowledge of data types helps prevent common bugs caused by unexpected type
conversions.