JavaScript9 min read

JavaScript Data Types

Understand JavaScript data types. Learn primitives, objects, type checking, and type coercion.

Alex Thompson
December 19, 2025
0.0k0

JavaScript Data Types

What Are Data Types?

Think of data types like different kinds of containers. You wouldn't put water in a box meant for books, right? JavaScript needs to know what kind of data you're storing so it knows how to work with it.

``` ┌─────────────────────┐ │ JavaScript sees: │ │ │ │ "John" → String │ ← Text │ 25 → Number │ ← Number │ true → Boolean │ ← Yes/No │ [1,2,3] → Array │ ← List │ {...} → Object │ ← Collection └─────────────────────┘ ```

The Main Types You'll Use

### 1. Numbers

Numbers are just... numbers. Whole numbers, decimals, negative numbers - all are numbers.

```javascript const age = 25; const price = 19.99; const temperature = -5; ```

JavaScript doesn't care if it's a whole number or decimal. It's all just a number.

### 2. Strings (Text)

Strings are text. Anything in quotes is a string.

```javascript const name = 'John'; const message = "Hello there"; const greeting = `Hi, ${name}`; ```

You can use single quotes, double quotes, or backticks. They all work the same for basic text. Backticks let you put variables inside.

### 3. Booleans (True or False)

Booleans are simple - just true or false. Like a light switch - it's either on or off.

```javascript const isLoggedIn = true; const hasPermission = false; const isAdult = age >= 18; // This becomes true or false ```

### 4. Arrays (Lists)

Arrays are lists of things. Like a shopping list.

```javascript const fruits = ['apple', 'banana', 'orange']; const numbers = [1, 2, 3, 4, 5]; ```

You put items in square brackets, separated by commas.

### 5. Objects (Collections)

Objects store related information together. Like a person's profile.

```javascript const person = { name: 'John', age: 25, city: 'New York' }; ```

You use curly braces and put key-value pairs inside.

How to Check What Type Something Is

Sometimes you need to know what type something is:

```javascript typeof 'hello'; // "string" typeof 42; // "number" typeof true; // "boolean" typeof [1, 2, 3]; // "object" (arrays are objects in JavaScript) ```

A Common Gotcha: Type Mixing

JavaScript sometimes converts types automatically, which can be confusing:

```javascript '5' + 3; // "53" (treats 3 as text, joins them) '5' - 3; // 2 (treats '5' as number, does math) ```

The plus sign joins strings together. Minus, multiply, and divide convert to numbers first.

Real Example: Working with Types

Let's say you're building a user profile:

```javascript // User's name (string) const userName = 'John';

// User's age (number) const userAge = 25;

// Is user active? (boolean) const isActive = true;

// User's hobbies (array) const hobbies = ['reading', 'coding', 'gaming'];

// User's address (object) const address = { street: '123 Main St', city: 'New York', zipCode: '10001' }; ```

Each piece of information has a type. JavaScript uses that type to know how to work with it.

Quick Reference

- **Number:** 1, 2.5, -10 - **String:** 'text', "text", `text` - **Boolean:** true, false - **Array:** [1, 2, 3] - **Object:** { key: 'value' }

That's the basics! You'll use these types constantly. Numbers for math, strings for text, booleans for yes/no questions, arrays for lists, objects for collections of related data.

#JavaScript#Data Types#Primitives#Beginner