JavaScript Data Types - Primitives & Reference Types
Master all JavaScript data types! Learn primitives (number, string, boolean, etc.) and reference types (objects, arrays, functions)!
Introduction
Data types define what kind of value a variable can hold. JavaScript has 7 primitive types and reference types! Let's learn them all!
What You Will Learn
- The 7 primitive data types
- Reference types (objects, arrays, functions)
- Differences between primitives and references
- Type checking with
typeof - Type conversion and coercion
Prerequisites
The 7 Primitive Types
Primitive types hold simple values, stored directly in memory!
- Number: Integers and floats
- String: Text
- Boolean: true or false
- Null: Intentional absence of value
- Undefined: Variable declared but not assigned
- Symbol: Unique identifier (ES6+)
- BigInt: Very large integers (ES2020+)
Number
let age = 30;
let price = 19.99;
let negative = -5;
let scientific = 1e6; // 1,000,000
let infinity = Infinity;
let notANumber = NaN; // Not a Number (still a Number type!)
String
let single = 'Hello';
let double = "World";
let template = `${single} ${double}!`; // Template literal!
let multiLine = `Line 1
Line 2
Line 3`; // Backticks allow multi-line!
Boolean
let isOnline = true;
let isAdmin = false;
Null & Undefined
let notAssigned; // undefined - declared, no value
let empty = null; // null - intentional nothingness
Symbol
let id1 = Symbol('id');
let id2 = Symbol('id');
console.log(id1 === id2); // false - every Symbol is unique!
BigInt
let big = 123456789012345678901234567890n; // Add 'n' at end!
Reference Types
Reference types hold references (addresses) to values in memory!
Object
let person = {
name: 'John',
age: 30,
isAdmin: true
};
Array
let numbers = [1, 2, 3, 4, 5];
let mixed = [1, 'hi', true, null];
Function
function sayHi() {
console.log('Hi!');
}
Primitives vs Reference Types: The Big Difference!
Primitives are copied by value
let a = 10;
let b = a;
a = 20;
console.log(b); // 10 - copied value, no change!
References are copied by reference
let obj1 = { x: 10 };
let obj2 = obj1;
obj1.x = 20;
console.log(obj2.x); // 20! Both point to same object!
Visual:
graph LR
A[obj1] -->|reference| C[Object { x: 20 }]
B[obj2] -->|reference| C
Type Checking
typeof Operator
console.log(typeof 5); // "number"
console.log(typeof 'hi'); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof {}); // "object"
console.log(typeof []); // "object" - wait, array is object!
console.log(typeof function(){}); // "function" - special case!
console.log(typeof null); // "object" - known bug in JS!
Check Array
console.log(Array.isArray([1,2,3])); // true
Type Conversion & Coercion
Explicit Conversion
Number('5'); // 5
String(123); // "123"
Boolean(1); // true
Implicit Coercion
console.log('5' + 3); // "53" - string concatenation!
console.log('5' - 3); // 2 - number subtraction!
console.log(5 + true); // 6 - true becomes 1
Best Practices
- Use strict equality (
===) not loose (==) to avoid coercion bugs! - Prefer explicit conversion over implicit!
- Use template literals for strings with variables!
Common Mistakes
- Confusing
==and===! - Forgetting arrays are objects!
- Modifying objects that are referenced elsewhere!
Summary
7 primitives, 3 main reference types! Primitives copy value, references copy the address!
Related Articles
Previous Tutorial
Next Tutorial
Let's learn type conversion! → Type Conversion & Coercion