Data Types

What Are Data Types in JavaScript?

Data types tell JavaScript what kind of value you are working with. Every value — whether it's a number, text, list, or object — belongs to a specific data type. Knowing them helps you write better, bug-free code.

Categories of Data Types

  • **Primitive Data Types** – Simple, single values that are immutable.
  • **Reference (Non-Primitive) Data Types** – Complex data structures stored by reference.

Primitive Data Types

Primitive types are stored directly in the variable and are immutable (cannot be changed directly). When copied, their value is duplicated — they are compared by value.

1. Number

Represents both integers and floating-point numbers. JavaScript has only one number type for all numeric values.

  • Integer: `let count = 42;`
  • Floating-point: `let price = 99.99;`
  • Infinity: `let inf = Infinity;`
  • Negative Infinity: `let negInf = -Infinity;`
  • NaN (Not-a-Number): Result of invalid math, e.g., `0 / 0`

let num1 = 10;      // integer
let num2 = 3.14;    // floating-point
let inf = Infinity; // infinite value
let notNum = NaN;   // invalid number

2. BigInt

Used for very large integers beyond the safe range of Number. Created by adding an `n` at the end of the number.

let big = 123456789012345678901234567890n;
let sum = big + 10n;

3. String

Represents text. Strings can be enclosed in single quotes `'`, double quotes `"`, or backticks `` ` `` (template literals).

let str1 = 'Hello';
let str2 = "World";
let name = `JD`;
console.log(`Hello ${name}`); // template literal

4. Boolean

Represents logical values — either `true` or `false`. Used for decision making.

let isOnline = true;
let isAdmin = false;

5. Null

Represents an intentional 'no value'. You assign `null` to indicate an empty value.

let selectedColor = null;

6. Undefined

A variable that has been declared but not assigned a value yet will be `undefined`.

let score; // undefined by default

7. Symbol

Represents a unique and immutable value. Often used as object keys to avoid property name collisions.

let sym1 = Symbol('id');
let sym2 = Symbol('id');
console.log(sym1 === sym2); // false

Reference (Non-Primitive) Data Types

Reference types store a reference (memory address) rather than the actual value. They are mutable and compared by reference.

  • **Object** – Collection of key-value pairs. [Read full Object topic](/languages/js/objects) *(Read in sequence — don't jump here directly)*
  • **Array** – Ordered list of values. [Read full Array topic](/languages/js/arrays) *(Read in sequence — don't jump here directly)*
  • **Function** – Block of reusable code. [Read full Function topic](/languages/js/functions) *(Read in sequence — don't jump here directly)*

// Object
let user = { name: 'JD', age: 25 };

// Array
let colors = ['red', 'green', 'blue'];

// Function
function greet() {
  console.log('Hello!');
}

The typeof Operator

The `typeof` operator tells you the type of a value or variable.

typeof 'JD';            // string
typeof 42;              // number
typeof true;            // boolean
typeof undefined;       // undefined
typeof null;            // object (quirk in JS)
typeof {a: 1};          // object
typeof [1, 2, 3];       // object
typeof function() {};   // function
typeof 123456789n;      // bigint
typeof Symbol('id');    // symbol

Primitive vs Reference Types

FeaturePrimitiveReference
Stored AsValue directlyMemory reference
MutabilityImmutableMutable
ComparisonBy valueBy reference
ExamplesNumber, String, Boolean, etc.Objects, Arrays, Functions

Type Conversion & Coercion

JavaScript can change a value's type automatically (coercion) or you can do it manually (conversion).

  • **Implicit Coercion** – JS changes type automatically, e.g., `5 + '5' → '55'`
  • **Explicit Conversion** – You manually change type using `String()`, `Number()`, `Boolean()` etc.

// Implicit coercion
console.log('5' + 2); // '52'
console.log('5' - 2); // 3

// Explicit conversion
let num = Number('123');
let str = String(123);

Summary

JavaScript data types fall into two categories: primitive and reference. Primitives store simple values and are immutable, while reference types store memory addresses and can hold complex data. Understanding these helps you write cleaner, bug-free code and work effectively with type conversions and comparisons.

JavaScript Data Types Explained

@JDCodebase • Learn JavaScript the right way 🚀