JS Data Structures
1. Arrays: The Ordered Workhorse
When to use them
Performance Profile
Comprehensive Operations Guide
const stack = ['Node', 'React', 'Vue'];
// --- 1. ADDING ITEMS ---
stack.push('Svelte'); // Add to End (Fast) -> Returns new length
stack.unshift('Angular'); // Add to Start (Slow) -> Returns new length
stack.splice(1, 0, 'Bun'); // Add to Middle (Slow) -> Insert at index 1
// --- 2. REMOVING ITEMS ---
const last = stack.pop(); // Remove End -> Returns item
const first = stack.shift(); // Remove Start -> Returns item
stack.splice(2, 1); // Remove 1 item at index 2
// --- 3. ACCESSING & FINDING ---
const item = stack[1]; // Standard access by index
const lastItem = stack.at(-1); // Modern way to get last item (ES2022)
const index = stack.indexOf('React'); // Find index (-1 if missing)
const hasIt = stack.includes('Vue'); // Boolean check (true/false)
// --- 4. ITERATION ---
// A. Standard Loop (Readable, supports 'break/continue')
for (const item of stack) {
console.log(item);
}
// B. Functional Loop (Cannot break, gives index)
stack.forEach((item, index) => {
console.log(`${index}: ${item}`);
});
// C. Transformation (Returns new array)
const upperStack = stack.map(tech => tech.toUpperCase());
2. Sets: The Unique Guard
When to use them
Performance Profile
Comprehensive Operations Guide
3. Maps: The Dictionary Powerhouse
When to use them
Performance Profile
Comprehensive Operations Guide
Summary: The Selection Cheat Sheet
Last updated