Skip to main content

What are JavaScript Data Types?

Answer

JavaScript has 8 data types, divided into primitive (7) and non-primitive (1) types.

Data Type Categories

Primitive Types

TypeExampleDescription
String"hello", 'world'Text data
Number42, 3.14, NaN, InfinityAll numbers (no int/float distinction)
Booleantrue, falseLogical values
undefinedundefinedUninitialized variables
nullnullIntentional absence of value
SymbolSymbol('id')Unique identifiers
BigInt9007199254740991nLarge integers
// Primitives
const str = "Hello"; // String
const num = 42; // Number
const bool = true; // Boolean
const undef = undefined; // Undefined
const nul = null; // Null
const sym = Symbol("unique"); // Symbol
const big = 9007199254740991n; // BigInt

// Check types with typeof
console.log(typeof str); // "string"
console.log(typeof num); // "number"
console.log(typeof bool); // "boolean"
console.log(typeof undef); // "undefined"
console.log(typeof nul); // "object" (historical bug!)
console.log(typeof sym); // "symbol"
console.log(typeof big); // "bigint"

Non-Primitive (Object)

// Objects
const obj = { name: "John", age: 30 };
const arr = [1, 2, 3];
const func = function () {
return "Hi";
};
const date = new Date();
const regex = /pattern/g;

// All are "object" type
console.log(typeof obj); // "object"
console.log(typeof arr); // "object"
console.log(typeof func); // "function" (special case)
console.log(typeof date); // "object"
console.log(typeof regex); // "object"

// Better type checking
console.log(Array.isArray(arr)); // true
console.log(obj instanceof Object); // true

Primitive vs Object

// Primitives are immutable (value copied)
let a = 5;
let b = a;
b = 10;
console.log(a); // 5 (unchanged)

// Objects are mutable (reference copied)
let obj1 = { x: 5 };
let obj2 = obj1;
obj2.x = 10;
console.log(obj1.x); // 10 (changed!)

Type Coercion

// JavaScript automatically converts types
console.log("5" + 3); // "53" (string)
console.log("5" - 3); // 2 (number)
console.log("5" * "2"); // 10 (number)
console.log(true + 1); // 2
console.log(false + 1); // 1

// Explicit conversion
console.log(String(123)); // "123"
console.log(Number("123")); // 123
console.log(Boolean(1)); // true
console.log(Boolean(0)); // false
console.log(Boolean("")); // false
console.log(Boolean("text")); // true

Falsy Values

// These are all falsy
if (!false) console.log("false is falsy");
if (!0) console.log("0 is falsy");
if (!"") console.log("empty string is falsy");
if (!null) console.log("null is falsy");
if (!undefined) console.log("undefined is falsy");
if (!NaN) console.log("NaN is falsy");

// Everything else is truthy
if ([]) console.log("empty array is truthy");
if ({}) console.log("empty object is truthy");
if ("0") console.log("string '0' is truthy");

Key Points

  • 7 primitive types + 1 object type
  • Primitives are immutable, objects are mutable
  • typeof null returns "object" (historical bug)
  • Use Array.isArray() for arrays
  • Be aware of type coercion
  • Know the 6 falsy values