JavaScript Data Types

JavaScript supports a variety of data types used to represent values in your code. Understanding these helps you store, compare, and operate on data correctly.

There are two main categories:

  • Primitive Types
  • Non-Primitive (Reference) Types

Primitive Data Types

These are basic, immutable values.

1. String : Represents text.

let name = "Alice";

2. Number : Represents both integers and floating-point numbers

let age = 30;
let price = 99.99;

3. Boolean : Represents true or false.

let isOnline = true;

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

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

5. Null : Represents an intentional absence of value.

let user = null;

6. Symbol (ES6) : Used to create unique identifiers.

let id = Symbol("userID");

7. BigInt (ES2020) : Used for large integers beyond the safe limit of Number.

let big = 1234567890123456789012345678901234567890n;

Non-Primitive (Reference) Types

These are complex types that can store multiple values.

1. Object

A collection of key-value pairs.

let person = { name: "Bob", age: 25 };

2. Array

An ordered list of values.

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

3. Function

A block of code designed to perform a task

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

typeof Operator

You can check the type of a value using typeof.

console.log(typeof "Hello");     // "string"
console.log(typeof 42);          // "number"
console.log(typeof true);        // "boolean"
console.log(typeof undefined);   // "undefined"
console.log(typeof null);        // "object" ← quirk in JavaScript
console.log(typeof []);          // "object"
console.log(typeof function(){}); // "function"

Important clarifications:

ValueTypeNotes
null“object”Legacy bug in JavaScript
[]“object”Use Array.isArray() to check array
NaN“number”Means “Not a Number”, still number

Summary

TypeExample
String“Hello”
Number100, 3.14
Booleantrue, false
Undefinedlet x;
Nulllet y = null;
SymbolSymbol(“desc”)
BigInt12345678901234567890n
Object{ name: “John”, age: 30 }
Array[1, 2, 3]
Functionfunction() {}

Best Practices

  • Use typeof to inspect data type, but use Array.isArray() for arrays.
  • Remember that null is technically an object.
  • Use BigInt only when needed for very large numbers.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.