DEV Community

Elayaraj C
Elayaraj C

Posted on

Understanding JavaScript Data Types day 1

🌟 Two Main Categories

JavaScript divides data types into two broad groups:

1️⃣ Primitive types (simple, immutable values)
2️⃣ Non-primitive types (objects, complex structures)

Let’s go through them.
πŸ”Ή Primitive Data Types

These are the most basic types β€” they hold a single value and are immutable (cannot be changed).
βœ… Number

For any numeric value, including decimals.

let age = 25;
let price = 99.99;

βœ… String

For text, wrapped in single ' or double " quotes.

let name = "Elayaraj";
let message = 'Hello, world!';

βœ… Boolean

True or false β€” used for logic and conditions.

let isActive = true;
let hasAccess = false;

βœ… Undefined

A variable that has been declared but not assigned a value.

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

βœ… Null

An intentional empty value.

let user = null;

βœ… Symbol

A unique and immutable value, often used as an object key (advanced use).

let id = Symbol("uniqueId");

βœ… BigInt

For very large integers beyond Number limits.

let bigNumber = 123456789012345678901234567890n;
Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Non-Primitive Data Types

These hold collections of values and are mutable.

βœ… Object

A collection of key-value pairs.

let person = {
  name: "Elayaraj",
  age: 25
};
Enter fullscreen mode Exit fullscreen mode

βœ… Array

An ordered list of values (technically an object).

let fruits = ["apple", "banana", "cherry"];
Enter fullscreen mode Exit fullscreen mode

βœ… Function

A reusable block of code (also a type of object).

function greet() {
  console.log("Hello!");
}

Enter fullscreen mode Exit fullscreen mode

🧠 Why Does This Matter?

Understanding data types helps you:

  • Write bug-free code (no type errors)
  • Use correct operations (e.g., adding numbers vs. concatenating strings)
  • Master type conversions (e.g., turning strings into numbers)

For example:

let x = "5" + 2;  // "52" (string concatenation)
let y = "5" - 2;  // 3   (number subtraction)
Enter fullscreen mode Exit fullscreen mode

πŸ” Type Checking

You can check a value’s type using:

typeof 42;        // "number"
typeof "hello";   // "string"
typeof true;      // "boolean"
typeof null;      // "object" (this is a known quirk!)
typeof [];        // "object"
typeof function() {}; // "function"

Enter fullscreen mode Exit fullscreen mode

Top comments (0)