LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 740 min read

JavaScript Data Types

Learn JavaScript data types in detail, including primitive and non-primitive types, dynamic typing, typeof, strings, numbers, bigint, booleans, undefined, null, symbols, objects, arrays, functions, and type checking.

Introduction

Every JavaScript program works with data. A user name is text, a product price is a number, a login status is a boolean, a collection of products may be an array, and a complete user profile may be represented as an object.

A data type describes the kind of value stored in a variable. The data type determines what the value represents, how JavaScript handles it, and which operations can be performed on it.

JavaScript has several built-in data types. Some are primitive values such as strings, numbers, booleans, undefined, null, bigint, and symbols. Other values, including objects, arrays, functions, and dates, belong to the broader object category.

Understanding data types is essential because almost every future JavaScript topic depends on them. Operators, type conversion, conditions, loops, functions, arrays, objects, DOM manipulation, JSON, storage, and APIs all work with different kinds of data.

What You Will Learn
  • What a data type is.
  • Why programming languages need data types.
  • The main categories of JavaScript data types.
  • The seven primitive JavaScript data types.
  • How non-primitive values work.
  • How JavaScript dynamic typing works.
  • How to use the typeof operator.
  • How strings store text.
  • How numbers represent numeric values.
  • What NaN and Infinity mean.
  • How BigInt stores very large integers.
  • How booleans represent true and false.
  • The difference between undefined and null.
  • Why typeof null returns object.
  • How symbols create unique values.
  • How objects store structured data.
  • How arrays, functions, and dates relate to objects.
  • The difference between primitive and reference behavior.
  • How to correctly check different data types.

What is a Data Type?

A data type is a classification that describes the kind of value being stored or processed by a program.

Different Types of Data
const name = 'Rahul';
const age = 22;
const isStudent = true;
console.log(name);
console.log(age);
console.log(isStudent);
Output

Click Run to see what this code prints.

Although all three values are stored in variables, they represent different kinds of information. The name variable stores text, age stores a numeric value, and isStudent stores a true-or-false value.

Data and Data Types
Value Data Type
--------------------------------
'Rahul' String
22 Number
true Boolean
undefined Undefined
null Null
123n BigInt
Symbol('id') Symbol
{ name: 'Rahul' } Object
Simple Definition
  • A value contains data.
  • A data type describes what kind of data the value represents.
  • Different data types support different operations.
  • JavaScript automatically determines the type of a value.

Why Do We Need Data Types?

Programs need to understand what kind of information they are processing. Adding two numbers is different from joining two pieces of text. A boolean is useful for making decisions, while an object is useful for storing related information.

Represent Information

Different data types represent text, numbers, logical values, structured records, and other forms of information.

Control Operations

The data type affects which operations can be performed and how operators behave.

Organize Data

Objects and arrays help organize complex collections of related values.

Validate Values

Type checking helps verify that a program receives the kind of data it expects.

Prevent Errors

Understanding types helps prevent unexpected calculations and incorrect comparisons.

Process Real Data

Forms, APIs, databases, and browser storage all provide values that must be handled correctly.

Type Affects Behavior
console.log(10 + 20);
console.log('10' + '20');
Output

Click Run to see what this code prints.

Mini Challenge · JavaScript

The two numbers should add up to 30, but they are written as strings so + concatenates them into "1020" instead. Fix the code so it logs the correct sum, 30.

expected output: 30

The first expression performs numeric addition because both values are numbers. The second expression joins text because both values are strings.

Real-World Analogy

Think of data types like different kinds of containers in a warehouse. Each container is designed for a particular kind of content.

Data Type Analogy
┌─────────────────────┐
│ Text Container │
│ 'JavaScript' │
│ Type: String │
└─────────────────────┘
┌─────────────────────┐
│ Number Container │
│ 27 │
│ Type: Number │
└─────────────────────┘
┌─────────────────────┐
│ Logic Container │
│ true │
│ Type: Boolean │
└─────────────────────┘
┌─────────────────────┐
│ Structured Container│
│ { name, age } │
│ Type: Object │
└─────────────────────┘

Just as different containers are suitable for different materials, different JavaScript data types are suitable for different kinds of information.

JavaScript Data Type Categories

JavaScript data types are commonly divided into two major categories: primitive data types and non-primitive data types.

JavaScript Data Types
JavaScript Data Types
│
├── Primitive Types
│ │
│ ├── String
│ ├── Number
│ ├── BigInt
│ ├── Boolean
│ ├── Undefined
│ ├── Null
│ └── Symbol
│
└── Non-Primitive Type
│
└── Object
│
├── Plain Objects
├── Arrays
├── Functions
├── Dates
├── Maps
├── Sets
└── Other Objects

Primitive Types

Simple values that are not objects and are generally treated as immutable values.

Non-Primitive Types

Objects that can store collections, structured information, behavior, and more complex data.

Primitive Data Types

Primitive data types represent basic values. JavaScript has seven primitive data types.

Seven Primitive Types
const text = 'JavaScript'; // String
const score = 100; // Number
const hugeNumber = 123456789n; // BigInt
const isActive = true; // Boolean
let result; // Undefined
const selectedItem = null; // Null
const uniqueId = Symbol('id'); // Symbol
Primitive Type List
1. String
2. Number
3. BigInt
4. Boolean
5. Undefined
6. Null
7. Symbol
Primitive Values
  • Primitive values are not objects.
  • Primitive values represent basic data.
  • Primitive values are immutable.
  • Variables containing primitive values behave independently when copied.

Non-Primitive Data Types

The main non-primitive type in JavaScript is Object. Objects can store multiple values and create complex data structures.

Non-Primitive Values
const user = {
name: 'Rahul',
age: 22
};
const technologies = [
'HTML',
'CSS',
'JavaScript'
];
function greet() {
console.log('Hello');
}
const today = new Date();
Object Category
Object
│
├── Plain Object
│ { name: 'Rahul' }
│
├── Array
│ ['HTML', 'CSS']
│
├── Function
│ function greet() {}
│
└── Date
new Date()

Arrays, functions, dates, maps, sets, and many other JavaScript structures are specialized forms of objects, although typeof reports functions as function.

Dynamic Typing

JavaScript is a dynamically typed language. This means a variable does not have one permanently fixed data type. The type belongs to the current value stored in the variable.

Dynamic Typing
let value = 'JavaScript';
console.log(typeof value);
value = 27;
console.log(typeof value);
value = true;
console.log(typeof value);
Output

Click Run to see what this code prints.

Mini Challenge · JavaScript

value should change from a string to a number to a boolean, but the second reassignment uses the string "27" instead of the number 27. Fix it so typeof correctly logs string, number, then boolean.

expected output: string number boolean
Type Changes
value
│
├── 'JavaScript' → string
│
├── 27 → number
│
└── true → boolean
Dynamic Typing Requires Care
  • A variable can hold different types at different times.
  • JavaScript determines types during execution.
  • Unexpected type changes can cause bugs.
  • Use clear variable names and intentional assignments.

The typeof Operator

The typeof operator returns a string describing the type of a value.

typeof Examples
console.log(typeof 'JavaScript');
console.log(typeof 27);
console.log(typeof 123n);
console.log(typeof true);
console.log(typeof undefined);
console.log(typeof Symbol('id'));
console.log(typeof {});
console.log(typeof function () {});
Output

Click Run to see what this code prints.

Using typeof with Variables
const courseName = 'JavaScript';
const totalLessons = 27;
const isFree = true;
console.log(typeof courseName);
console.log(typeof totalLessons);
console.log(typeof isFree);
Output

Click Run to see what this code prints.

typeof Returns Text
  • typeof returns a string.
  • typeof 10 returns "number".
  • typeof true returns "boolean".
  • typeof {} returns "object".
  • typeof null unexpectedly returns "object".

1. String

A string represents textual data. Strings can contain letters, numbers, spaces, symbols, words, sentences, or any other sequence of characters.

String Values
const firstName = 'Rahul';
const course = "JavaScript";
const message = `Welcome to PrograMinds`;
console.log(firstName);
console.log(course);
console.log(message);
Output

Click Run to see what this code prints.

Checking String Type
const language = 'JavaScript';
console.log(typeof language);
Output

Click Run to see what this code prints.

Creating Strings

JavaScript strings can be created using single quotes, double quotes, or backticks.

Three String Styles
const singleQuote = 'JavaScript';
const doubleQuote = "JavaScript";
const templateLiteral = `JavaScript`;
console.log(singleQuote);
console.log(doubleQuote);
console.log(templateLiteral);
Output

Click Run to see what this code prints.

Backticks create template literals, which support embedded expressions and multi-line strings.

Template Literal
const name = 'Rahul';
const course = 'JavaScript';
const message = `Hello ${name}, welcome to ${course}!`;
console.log(message);
Output

Click Run to see what this code prints.

String Examples

Different String Values
const name = 'Rahul';
const city = 'Mumbai';
const phone = '9876543210';
const emptyText = '';
const sentence = 'JavaScript is powerful!';
console.log(typeof name);
console.log(typeof city);
console.log(typeof phone);
console.log(typeof emptyText);
console.log(typeof sentence);
Output

Click Run to see what this code prints.

Important
  • A numeric-looking value inside quotes is still a string.
  • An empty string is still a string.
  • Spaces and symbols can be part of strings.
  • Strings will be covered in detail in Lesson 16.

2. Number

The Number data type represents numeric values. JavaScript uses the same Number type for most integers and decimal numbers.

Number Values
const age = 22;
const price = 499.99;
const temperature = -5;
const zero = 0;
console.log(typeof age);
console.log(typeof price);
console.log(typeof temperature);
console.log(typeof zero);
Output

Click Run to see what this code prints.

JavaScript Number Type
  • Most whole numbers use the Number type.
  • Decimal values also use the Number type.
  • Negative numbers use the Number type.
  • Special values such as NaN and Infinity also have typeof "number".

Integer and Decimal Numbers

Unlike some programming languages, JavaScript does not normally use separate basic types such as int, float, and double for regular numeric values.

Integer and Decimal
const totalStudents = 100;
const productPrice = 499.99;
console.log(typeof totalStudents);
console.log(typeof productPrice);
Output

Click Run to see what this code prints.

JavaScript Number
100 ─────┐
│
499.99 ───────┼──▶ Number
│
-25 ───────┤
│
0 ───────┘

Special Number Values

JavaScript includes special numeric values such as NaN, Infinity, and negative Infinity.

Special Numbers
const invalidNumber = NaN;
const positiveInfinity = Infinity;
const negativeInfinity = -Infinity;
console.log(typeof invalidNumber);
console.log(typeof positiveInfinity);
console.log(typeof negativeInfinity);
Output

Click Run to see what this code prints.

NaN

NaN means Not-a-Number. It represents an invalid or unsuccessful numeric result, but its JavaScript type is still number.

Creating NaN
const result = 0 / 0;
console.log(result);
console.log(typeof result);
Output

Click Run to see what this code prints.

Mini Challenge · JavaScript

The result should be NaN because dividing 0 by 0 is mathematically undefined, but the code divides by 1 instead. Fix it so the result is NaN.

expected output: NaN number
Invalid Numeric Operation
const result = Number('Hello');
console.log(result);
Output

Click Run to see what this code prints.

NaN has unusual comparison behavior. It is not equal to itself.

NaN Comparison
console.log(NaN === NaN);
Output

Click Run to see what this code prints.

Checking NaN
const result = Number('Hello');
console.log(Number.isNaN(result));
Output

Click Run to see what this code prints.

Infinity

Infinity represents a numeric value greater than any finite number. JavaScript also supports negative Infinity.

Infinity Example
const positive = 1 / 0;
const negative = -1 / 0;
console.log(positive);
console.log(negative);
Output

Click Run to see what this code prints.

Infinity Type
console.log(typeof Infinity);
Output

Click Run to see what this code prints.

3. BigInt

BigInt represents integer values that may be larger than the safely representable integer range of the Number type.

BigInt Value
const hugeNumber = 123456789012345678901234567890n;
console.log(hugeNumber);
console.log(typeof hugeNumber);
Output

Click Run to see what this code prints.

A BigInt literal is commonly created by placing the letter n after an integer.

Creating BigInt
const value1 = 100n;
const value2 = BigInt(200);
console.log(value1);
console.log(value2);
Output

Click Run to see what this code prints.

BigInt Limitation
  • BigInt represents integers.
  • A BigInt literal ends with n.
  • BigInt and Number cannot be freely mixed in arithmetic.
  • Convert values explicitly when appropriate.

Number vs BigInt

Number vs BigInt
Feature Number BigInt
------------------------------------------------
Example 100 100n
typeof number bigint
Decimals Yes No
Very Large Integers Limited Supported
Literal Suffix None n
Cannot Directly Mix Types
const regularNumber = 10;
const bigNumber = 20n;
console.log(regularNumber + bigNumber);
Result

Click Run to see what this code prints.

4. Boolean

The Boolean data type has only two possible values: true and false.

Boolean Values
const isLoggedIn = true;
const hasPermission = false;
console.log(isLoggedIn);
console.log(hasPermission);
console.log(typeof isLoggedIn);
Output

Click Run to see what this code prints.

Booleans are commonly used for decisions, conditions, status flags, validation results, and application state.

Authentication

Represent whether a user is logged in.

Visibility

Represent whether an element should be shown.

Validation

Represent whether input is valid.

Settings

Represent enabled or disabled options.

Boolean Values from Comparisons

Comparison expressions produce boolean results.

Comparison Results
console.log(10 > 5);
console.log(10 < 5);
console.log(10 === 10);
console.log(10 === 20);
Output

Click Run to see what this code prints.

Mini Challenge · JavaScript

The comparisons should show that 10 is greater than 5 and not less than 5, but the operators are swapped. Fix them so the output is true, false, true, false.

expected output: true false true false
Store Comparison Result
const age = 22;
const isAdult = age >= 18;
console.log(isAdult);
Output

Click Run to see what this code prints.

5. Undefined

undefined represents the absence of an assigned value. A variable declared with let or var but not given a value automatically contains undefined.

Undefined Variable
let userName;
console.log(userName);
console.log(typeof userName);
Output

Click Run to see what this code prints.

Undefined State
let userName;
│
▼
Variable Exists
│
▼
No Value Assigned
│
▼
undefined

A function that does not explicitly return a value also returns undefined.

Undefined Function Result
function greet() {
console.log('Hello');
}
const result = greet();
console.log(result);
Output

Click Run to see what this code prints.

6. Null

null represents an intentional absence of a value. Developers commonly assign null when a variable deliberately has no meaningful object or value at the moment.

Null Value
const selectedProduct = null;
console.log(selectedProduct);
Output

Click Run to see what this code prints.

Real-World Null Example
let currentUser = null;
console.log(currentUser);
// Later
currentUser = {
name: 'Rahul'
};
console.log(currentUser);
Output

Click Run to see what this code prints.

Undefined vs Null

undefined and null both represent missing values, but they are normally used differently.

Undefined vs Null
Feature undefined null
-------------------------------------------------
Meaning Not assigned Intentionally empty
Common Source JavaScript Developer
typeof undefined object
Primitive? Yes Yes
Example let value; let value = null;
Comparison
let firstValue;
let secondValue = null;
console.log(firstValue);
console.log(secondValue);
Output

Click Run to see what this code prints.

Simple Difference
  • undefined usually means a value has not been assigned.
  • null usually means no value was intentionally assigned.
  • Both represent absence in different ways.
  • Their types and comparison behavior differ.

The typeof null Behavior

One of the most famous unusual behaviors in JavaScript is that typeof null returns object.

typeof null
console.log(typeof null);
Output

Click Run to see what this code prints.

Despite this output, null is a primitive value. The result is a historical behavior preserved for compatibility with existing JavaScript code.

Remember
  • null is a primitive value.
  • typeof null returns "object".
  • This is a historical JavaScript behavior.
  • Check null directly using value === null.

7. Symbol

A Symbol is a primitive value designed to create unique identifiers. Symbols are often used as unique object property keys and in advanced JavaScript features.

Creating a Symbol
const id = Symbol('id');
console.log(id);
console.log(typeof id);
Output

Click Run to see what this code prints.

The text passed to Symbol is an optional description that helps developers identify the symbol during debugging.

Symbol Uniqueness

Every Symbol() call creates a new unique symbol, even when the descriptions are identical.

Unique Symbols
const id1 = Symbol('id');
const id2 = Symbol('id');
console.log(id1 === id2);
Output

Click Run to see what this code prints.

Symbol as Object Key
const userId = Symbol('userId');
const user = {
name: 'Rahul',
[userId]: 101
};
console.log(user[userId]);
Output

Click Run to see what this code prints.

8. Object

An object stores related data using key-value pairs. Objects are one of the most important data structures in JavaScript.

Object Example
const student = {
name: 'Rahul',
age: 22,
course: 'JavaScript',
isActive: true
};
console.log(student);
Output

Click Run to see what this code prints.

Checking Object Type
const student = {
name: 'Rahul'
};
console.log(typeof student);
Output

Click Run to see what this code prints.

Object Properties

Each object property contains a key and a value. Different properties can contain different data types.

Different Property Types
const course = {
name: 'JavaScript',
totalLessons: 27,
isFree: true,
instructor: null,
topics: ['Variables', 'Constants', 'Data Types']
};
console.log(course);
Object Structure
course
│
├── name → String
├── totalLessons → Number
├── isFree → Boolean
├── instructor → Null
└── topics → Array

Accessing Object Values

Object values can commonly be accessed using dot notation or bracket notation.

Accessing Properties
const student = {
name: 'Rahul',
age: 22
};
console.log(student.name);
console.log(student['age']);
Output

Click Run to see what this code prints.

Objects will be covered in detail in Lesson 18.

Arrays

An array is a specialized object used to store an ordered collection of values.

Array Example
const technologies = [
'HTML',
'CSS',
'JavaScript'
];
console.log(technologies);
console.log(typeof technologies);
Output

Click Run to see what this code prints.

Because arrays are specialized objects, typeof an array returns object.

Correct Array Check
const technologies = ['HTML', 'CSS'];
console.log(Array.isArray(technologies));
Output

Click Run to see what this code prints.

Array Type Checking
  • typeof [] returns "object".
  • Use Array.isArray() to check whether a value is an array.
  • Arrays can store values of different types.
  • Arrays will be covered in detail in Lesson 17.

Functions

Functions are callable objects in JavaScript. The typeof operator returns function for function values.

Function Type
function greet() {
return 'Hello';
}
console.log(typeof greet);
Output

Click Run to see what this code prints.

Function Stored in a Variable
const add = function (a, b) {
return a + b;
};
console.log(typeof add);
console.log(add(10, 20));
Output

Click Run to see what this code prints.

Functions will be covered in detail in Lesson 13.

Dates

JavaScript provides the Date object for working with dates and times.

Date Object
const today = new Date();
console.log(typeof today);
console.log(today instanceof Date);
Output

Click Run to see what this code prints.

The Date object will be covered in detail in Lesson 19.

Primitive vs Reference Values

Primitive and object values behave differently when variables are copied. This difference is essential for understanding JavaScript programs.

Primitive vs Reference
Primitive Values
--------------------------------
String
Number
BigInt
Boolean
Undefined
Null
Symbol
Copied as independent values.
Reference Values
--------------------------------
Objects
Arrays
Functions
Dates
Variables may refer to the same object.

Primitive Behavior

Copying a primitive value creates an independent value for the new variable.

Reference Behavior

Copying an object reference can make two variables refer to the same object.

Copying Primitive Values

When a primitive value is copied from one variable to another, changing one variable does not change the other.

Primitive Copy
let firstScore = 100;
let secondScore = firstScore;
secondScore = 200;
console.log(firstScore);
console.log(secondScore);
Output

Click Run to see what this code prints.

Independent Primitive Values
Initial State
firstScore → 100
Copy
secondScore → 100
After Change
firstScore → 100
secondScore → 200
Independent Values ✅

Copying Reference Values

When an object reference is copied, both variables can refer to the same object. A mutation through one variable can therefore be visible through the other.

Object Reference Copy
const firstUser = {
name: 'Rahul'
};
const secondUser = firstUser;
secondUser.name = 'Amit';
console.log(firstUser.name);
console.log(secondUser.name);
Output

Click Run to see what this code prints.

Mini Challenge · JavaScript

Both firstUser and secondUser should reflect the update because they reference the same object, but the code creates a brand-new object for secondUser instead of copying the reference. Fix it so both variables show "Amit".

expected output: Amit Amit
Shared Reference
firstUser ───────┐
│
▼
┌──────────────┐
│ name: Rahul │
└──────────────┘
▲
│
secondUser ──────┘
After Mutation
┌──────────────┐
│ name: Amit │
└──────────────┘
Both variables observe the change.
Important Difference
  • Primitive values are copied independently.
  • Objects are accessed through references.
  • Two variables can refer to the same object.
  • Mutating the shared object affects what both variables observe.

Checking Data Types

JavaScript provides several techniques for checking the type or structure of a value.

Using typeof
const name = 'Rahul';
const age = 22;
const isActive = true;
console.log(typeof name === 'string');
console.log(typeof age === 'number');
console.log(typeof isActive === 'boolean');
Output

Click Run to see what this code prints.

Multiple Type Checks
function showType(value) {
console.log('Value:', value);
console.log('Type:', typeof value);
}
showType('JavaScript');
showType(27);
showType(true);
Output

Click Run to see what this code prints.

Checking Arrays

The typeof operator cannot distinguish a normal object from an array because both return object.

typeof Limitation
console.log(typeof {});
console.log(typeof []);
Output

Click Run to see what this code prints.

Use Array.isArray() when you specifically need to check whether a value is an array.

Array.isArray()
const user = {
name: 'Rahul'
};
const technologies = [
'HTML',
'CSS',
'JavaScript'
];
console.log(Array.isArray(user));
console.log(Array.isArray(technologies));
Output

Click Run to see what this code prints.

Mini Challenge · JavaScript

Array.isArray() should correctly identify the array, but the second check uses typeof, which never returns "array". Fix the code to use Array.isArray() for both checks so it prints false then true.

expected output: false true

Checking null

Because typeof null returns object, the safest simple way to check specifically for null is strict equality.

Checking null
const selectedUser = null;
console.log(selectedUser === null);
Output

Click Run to see what this code prints.

Object and Null Check
function isObject(value) {
return typeof value === 'object' && value !== null;
}
console.log(isObject({}));
console.log(isObject(null));
Output

Click Run to see what this code prints.

JavaScript Data Type Comparison

Data Type Comparison Table
Value Data Type typeof Result
---------------------------------------------------------
'JavaScript' String string
27 Number number
27.5 Number number
123n BigInt bigint
true Boolean boolean
false Boolean boolean
undefined Undefined undefined
null Null object
Symbol('id') Symbol symbol
{} Object object
[] Array object
function () {} Function function
new Date() Date object
Type Checking Summary
  • Use typeof for most primitive types.
  • Use Array.isArray() for arrays.
  • Use value === null for null.
  • Use Number.isNaN() for NaN.
  • Use instanceof when checking certain object instances such as Date.
  • Remember that typeof null returns "object".

Complete Data Types Example

The following example combines the major JavaScript data types in a simple course management program.

course-data.js
// String
const courseName = 'Core JavaScript';
// Number
const totalLessons = 27;
// BigInt
const courseId = 9007199254740993n;
// Boolean
const isFree = true;
// Undefined
let certificateNumber;
// Null
let currentInstructor = null;
// Symbol
const internalId = Symbol('courseId');
// Object
const course = {
name: courseName,
lessons: totalLessons,
free: isFree
};
// Array
const completedTopics = [
'Introduction',
'History',
'JavaScript Engine',
'How JavaScript Works',
'Variables',
'Constants'
];
// Function
function showCourse() {
console.log('Course:', course.name);
console.log('Lessons:', course.lessons);
console.log('Free:', course.free);
}
console.log('Course Name Type:', typeof courseName);
console.log('Lessons Type:', typeof totalLessons);
console.log('Course ID Type:', typeof courseId);
console.log('Free Type:', typeof isFree);
console.log('Certificate Type:', typeof certificateNumber);
console.log('Instructor:', currentInstructor);
console.log('Symbol Type:', typeof internalId);
console.log('Course Type:', typeof course);
console.log('Is Topics Array:', Array.isArray(completedTopics));
console.log('Function Type:', typeof showCourse);
showCourse();
Output

Click Run to see what this code prints.

Program Data Structure
Course Application
│
├── courseName ───────▶ String
├── totalLessons ─────▶ Number
├── courseId ─────────▶ BigInt
├── isFree ───────────▶ Boolean
├── certificateNumber ▶ Undefined
├── currentInstructor ▶ Null
├── internalId ───────▶ Symbol
├── course ───────────▶ Object
├── completedTopics ──▶ Array
└── showCourse ───────▶ Function

Real-World Applications

Every real JavaScript application combines multiple data types.

User Profiles

Strings store names, numbers store ages, booleans store status, and objects combine the complete profile.

E-Commerce

Numbers store prices, strings store product names, arrays store products, and objects represent individual products.

Authentication

Booleans represent login state while null may represent the absence of a current user.

API Data

API responses commonly contain strings, numbers, booleans, null values, arrays, and objects.

Forms

Form inputs commonly begin as strings and may require conversion into numbers or other types.

Games

Numbers store scores, booleans store game states, and objects represent players.

Dashboards

Numbers represent statistics while arrays and objects organize complex datasets.

Browser Storage

Storage systems work with strings and often require objects to be converted to and from JSON.

Real User Data
const user = {
id: 101,
name: 'Rahul',
email: 'rahul@example.com',
isLoggedIn: true,
lastLogin: null,
skills: ['HTML', 'CSS', 'JavaScript']
};
console.log(user);

Common Beginner Mistakes

Avoid These Mistakes
  • Thinking numbers inside quotes are Number values.
  • Assuming JavaScript has separate int and float primitive types.
  • Thinking NaN has a type other than number.
  • Comparing NaN using NaN === NaN.
  • Mixing BigInt and Number directly in arithmetic.
  • Thinking undefined and null are exactly the same.
  • Assuming typeof null returns null.
  • Thinking null is actually an object because typeof returns object.
  • Using typeof to distinguish arrays from normal objects.
  • Forgetting that functions return typeof "function".
  • Thinking arrays are primitive values.
  • Thinking objects are copied independently like primitives.
  • Mutating a shared object without realizing another variable refers to it.
  • Changing variable types unnecessarily because JavaScript is dynamically typed.
  • Using typeof value === "array".
  • Using typeof alone to check for null.
  • Assuming all objects are plain objects.
  • Ignoring unexpected types received from forms, APIs, or storage.
Common Mistakes
// ❌ This is a string, not a number
const age = '22';
// ❌ NaN is not equal to itself
console.log(NaN === NaN);
// ❌ typeof does not return "array"
console.log(typeof []);
// ❌ typeof null returns "object"
console.log(typeof null);
// ❌ Cannot directly mix Number and BigInt
console.log(10 + 20n);

Best Practices

  • Understand the type of data stored in each variable.
  • Use meaningful variable names that indicate the purpose of the value.
  • Use typeof for basic type checking.
  • Use Array.isArray() when checking arrays.
  • Use value === null when checking specifically for null.
  • Use Number.isNaN() when checking for NaN.
  • Do not assume a numeric-looking string is a number.
  • Convert external data explicitly when necessary.
  • Avoid unnecessary changes between unrelated data types.
  • Use booleans for true-or-false states.
  • Use null intentionally when representing an expected absence of value.
  • Understand when undefined is produced automatically.
  • Use BigInt only when large integer requirements justify it.
  • Do not directly mix BigInt and Number arithmetic.
  • Remember that arrays and functions belong to the object family.
  • Understand primitive copying behavior.
  • Understand shared references before modifying objects and arrays.
  • Validate data received from users, APIs, browser storage, and external systems.
  • Do not rely on typeof alone for every type-checking situation.
  • Choose data structures according to the information being represented.
Recommended Type Checks
const name = 'Rahul';
const age = 22;
const technologies = ['HTML', 'CSS'];
const selectedUser = null;
console.log(typeof name === 'string');
console.log(typeof age === 'number');
console.log(Array.isArray(technologies));
console.log(selectedUser === null);
Output

Click Run to see what this code prints.

Frequently Asked Questions

A data type describes the kind of value stored or processed by a program.

JavaScript has seven primitive data types: String, Number, BigInt, Boolean, Undefined, Null, and Symbol.

Object is the main non-primitive data type in JavaScript.

Yes. A variable can store values of different types at different times.

The typeof operator returns a string describing the type of a value.

It returns "string".

It returns "number".

No. Regular integers and decimal values both normally use the Number type.

NaN means Not-a-Number and represents an invalid numeric result.

typeof NaN returns "number".

Number.isNaN() is commonly used to check whether a value is the NaN value.

BigInt is a primitive type used for integer values that may exceed the safely representable integer range of Number.

No. They generally require explicit conversion to compatible types before arithmetic.

A Boolean is a data type with only two values: true and false.

undefined commonly represents a variable or result for which no value has been assigned.

null commonly represents an intentional absence of a value.

No. They are different primitive values with different typical meanings and types.

It is a historical JavaScript behavior preserved for compatibility. null itself is still a primitive value.

A Symbol is a primitive value commonly used to create unique identifiers.

No. Each normal Symbol() call creates a unique value.

An object is a non-primitive value that can store related information using properties.

Yes. Arrays are specialized objects used for ordered collections.

Arrays belong to the object category in JavaScript.

Use Array.isArray(value).

It returns "function".

Yes. Copying a primitive value gives the new variable an independent primitive value.

Yes. Copying an object reference can make both variables refer to the same object.

Use strict equality, such as value === null.

Key Takeaways

  • A data type describes the kind of value being stored or processed.
  • JavaScript has primitive and non-primitive data types.
  • JavaScript has seven primitive data types.
  • The primitive types are String, Number, BigInt, Boolean, Undefined, Null, and Symbol.
  • Object is the main non-primitive type.
  • Arrays, functions, and dates belong to the broader object family.
  • JavaScript is dynamically typed.
  • The type belongs to the current value, not permanently to the variable.
  • The typeof operator helps inspect data types.
  • Strings represent textual data.
  • Numbers represent regular numeric values.
  • JavaScript normally uses Number for both integers and decimals.
  • NaN represents an invalid numeric result.
  • typeof NaN returns "number".
  • Infinity and -Infinity are special Number values.
  • BigInt represents large integer values.
  • BigInt literals commonly end with n.
  • Number and BigInt cannot be directly mixed in normal arithmetic.
  • Booleans contain true or false.
  • undefined commonly represents an unassigned value.
  • null commonly represents an intentional absence of value.
  • typeof null returns "object" because of historical behavior.
  • null is still a primitive value.
  • Symbols create unique primitive values.
  • Objects store structured information using properties.
  • typeof an array returns "object".
  • Use Array.isArray() to identify arrays.
  • typeof a function returns "function".
  • Primitive values behave independently when copied.
  • Copied object references can refer to the same object.
  • Mutating a shared object can be observed through multiple variables.
  • Use appropriate type-checking techniques for different values.

Summary

Data types are fundamental to JavaScript because every value belongs to a particular type. The type determines what kind of information the value represents and how JavaScript can work with it.

JavaScript has seven primitive data types: String, Number, BigInt, Boolean, Undefined, Null, and Symbol. These types represent basic values such as text, numeric information, logical states, missing values, and unique identifiers.

Objects are the main non-primitive type. Plain objects, arrays, functions, dates, maps, sets, and many other structures belong to the broader object system. Objects allow JavaScript programs to represent complex and structured information.

JavaScript is dynamically typed, which means a variable can hold values of different types at different times. This flexibility is powerful, but developers must understand and manage types carefully to avoid unexpected behavior.

The typeof operator is useful for identifying many types, but it has important limitations. typeof null returns object, and typeof arrays also returns object. Therefore, null should be checked directly and arrays should be checked using Array.isArray().

Primitive and reference behavior is another essential distinction. Primitive values behave independently when copied, while multiple variables can refer to the same object. Mutating a shared object can therefore affect what multiple variables observe.

Lesson 7 Completed
  • You understand what data types are.
  • You know why programs need different data types.
  • You know the primitive and non-primitive categories.
  • You know all seven primitive JavaScript data types.
  • You understand JavaScript dynamic typing.
  • You know how to use the typeof operator.
  • You understand strings and numbers.
  • You understand NaN and Infinity.
  • You understand the purpose of BigInt.
  • You understand boolean values.
  • You know the difference between undefined and null.
  • You understand the unusual typeof null behavior.
  • You understand Symbol values.
  • You understand objects and object properties.
  • You know that arrays are specialized objects.
  • You know how functions and dates are classified.
  • You understand primitive and reference behavior.
  • You know how to check common JavaScript data types.
  • You are ready to learn JavaScript Type Conversion.
Next Lesson →

JavaScript Type Conversion