JavaScript

Chunk Array

by @admin
12h ago
Apr 28, 2026
Public
Splits an array into smaller arrays (chunks) of a specified size. The last chunk may be smaller if the array length is not evenly divisible. Useful for batch processing, pagination, rendering large lists in chunks, and rate-limited API calls.
JavaScript
function chunk(array, size) {
  const chunks = [];
  for (let i = 0; i < array.length; i += size) {
    chunks.push(array.slice(i, i + size));
  }
  return chunks;
}

// Usage
const items = [1, 2, 3, 4, 5, 6, 7];
console.log(chunk(items, 3));
// [[1,2,3], [4,5,6], [7]]
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.