JavaScript Data Types Explained with Examples

Himmat Kumar Mar 25, 2025, 5:47 PM
Blog Thumbnail

JavaScript Data Types Explained

In JavaScript, data types define the kind of data a variable can hold. Understanding data types helps you write better, bug-free code. Let's explore the two main categories: Primitive and Reference types.

1. Primitive Data Types

Primitive types are basic, immutable values stored directly in memory. JavaScript has 7 of them:

  • String
  • Number
  • Boolean
  • Null
  • Undefined
  • Symbol (ES6)
  • BigInt (ES11)

πŸ”€ String

Used for textual data.

let name = "Alice";
let greeting = 'Hello!';
let message = `Hi, ${name}`;

πŸ”’ Number

Used for both integers and decimals.

let age = 30;
let price = 99.99;

βœ… Boolean

Represents either true or false.

let isLoggedIn = true;
let isAdmin = false;

❌ Null

Intentional absence of any value.

let user = null;

πŸŒ€ Undefined

A variable declared but not assigned a value.

let score;
console.log(score); // undefined

πŸ”£ Symbol

A unique and immutable primitive value, often used as object keys.

const id = Symbol("id");
const id2 = Symbol("id");
console.log(id === id2); // false

πŸ”’ BigInt

Used for very large integers beyond the safe integer limit.

const bigNumber = 1234567890123456789012345678901234567890n;

2. Reference Data Types

Reference types are objects stored as references in memory.

  • Object
  • Array
  • Function
  • Date, RegExp, etc.

πŸ“¦ Object

const user = {
  name: "Alice",
  age: 25
};

πŸ“š Array

const colors = ["red", "green", "blue"];

πŸ”§ Function

function greet(name) {
  return `Hello, ${name}`;
}

3. Checking Data Types

Use the typeof operator to check a variable's type.

typeof "hello"     // string
typeof 123         // number
typeof false       // boolean
typeof undefined   // undefined
typeof null        // object (quirk!)
typeof {}          // object
typeof []          // object
typeof function(){} // function

4. Summary Table

Data Type Category Example
String Primitive "Hello"
Number Primitive 123, 45.6
Boolean Primitive true / false
Null Primitive null
Undefined Primitive undefined
Object Reference { name: "John" }
Array Reference [1, 2, 3]
βœ… Pro Tip: Always use typeof to safely check your variable types!

Comments

Please login to leave a comment.

No comments yet.

Related Posts

Himmat Kumar β€’ Oct 27, 2023, 11:36 AM

Introduction JavaScript

Himmat Kumar β€’ Oct 21, 2024, 12:05 PM

Difference between javascript and jquery with examples

Himmat Kumar β€’ Mar 25, 2025, 12:03 PM

JavaScript Variables: var vs let vs const

Himmat Kumar β€’ Mar 25, 2025, 12:31 PM

JavaScript Operators Explained | Arithmetic, Comparison...

Himmat Kumar β€’ Mar 29, 2025, 5:32 AM

JavaScript Conditional Statements – if, else, switch

Himmat Kumar β€’ Mar 29, 2025, 5:43 AM

JavaScript Loops β€” for, while, do...while

Himmat Kumar β€’ Mar 30, 2025, 5:26 AM

JavaScript Functions & Scope

Himmat Kumar β€’ Mar 30, 2025, 6:46 AM

Date and Time in JavaScript β€” Easy Guide

10032 viewsnextjs
Himmat Regar β€’ Jun 27, 2025, 11:09 AM

Next.js vs React: What’s the Difference and When to Use...