Example 1 — Official Samples
Skip 1, 2, 3, and 4 items from a list, then 3 characters from a string — straight from the Ramda docs.
const words = ['foo', 'bar', 'baz'];
console.log(R.drop(1, words)); // ['bar', 'baz']
console.log(R.drop(2, words)); // ['baz']
console.log(R.drop(3, words)); // []
console.log(R.drop(4, words)); // []
console.log(R.drop(3, 'ramda')); // 'da'
["bar", "baz"]
["baz"]
[]
[]
"da"
How It Works
- Skip from the start.
R.drop(1, words) removes 'foo' and keeps the rest. - Bigger n. Dropping 2 leaves only
'baz'; dropping 3 removes everything. - Past the end. Dropping 4 from a 3-item list is not an error — you just get
[]. - Strings. A string returns a string: skipping
'ram' leaves 'da'.
Example 3 — Paginate with drop and take
Skip the earlier pages with R.drop, then keep one page with R.take.
const items = [1, 2, 3, 4, 5, 6, 7, 8];
const getPage = (page, size) =>
R.pipe(R.drop((page - 1) * size), R.take(size));
console.log(getPage(1, 3)(items)); // [1, 2, 3]
console.log(getPage(2, 3)(items)); // [4, 5, 6]
console.log(getPage(3, 3)(items)); // [7, 8]
[1, 2, 3]
[4, 5, 6]
[7, 8]
How It Works
- Items to skip. Page 2 with size 3 skips
(2 - 1) * 3 = 3 items. - Drop.
R.drop(3) turns the list into [4, 5, 6, 7, 8]. - Take.
R.take(3) keeps the first three: [4, 5, 6]. - Last page. Page 3 has only two items left, so
R.take(3) safely returns [7, 8].
Example 4 — Zero, Negative, and Too-Large n
R.drop never throws for an odd n; it clamps it to the list instead.
const nums = [1, 2, 3];
console.log(R.drop(0, nums)); // [1, 2, 3]
console.log(R.drop(-2, nums)); // [1, 2, 3]
console.log(R.drop(10, nums)); // []
console.log(R.drop(2, [])); // []
[1, 2, 3]
[1, 2, 3]
[]
[]
How It Works
- Zero. Skipping nothing returns a new copy with every item.
- Negative. Ramda uses
Math.max(0, n), so -2 becomes 0 — it does not count from the end. - Too large. Skipping 10 from 3 items leaves nothing:
[]. - Empty list. There is nothing to skip, so the result is another empty array.
Example 5 — drop vs dropLast vs tail
Three ways to remove items — from the start, from the end, or exactly one.
const nums = [1, 2, 3, 4, 5];
console.log(R.drop(2, nums)); // [3, 4, 5]
console.log(R.dropLast(2, nums)); // [1, 2, 3]
console.log(R.tail(nums)); // [2, 3, 4, 5]
[3, 4, 5]
[1, 2, 3]
[2, 3, 4, 5]
How It Works
- drop. Removes the first two items,
1 and 2. - dropLast.
R.dropLast removes the last two, 4 and 5. - tail.
R.tail always removes exactly one item from the start — the same as R.drop(1). - All new. Each call returns a fresh array;
nums is still [1, 2, 3, 4, 5].