Lodash _.flip() method
What you’ll learn
- How
_.flipreverses arguments passed into the wrapped function. - How flip differs from
_.reargand from mutating arrays. - Practical patterns with binary helpers and variadic functions.
- How to avoid surprises with arity and third-party callbacks.
Prerequisites
Optional read: _.delay(), _.curryRight(), and the Function hub.
- First-class functions: passing functions as values and returning new functions.
- The arguments object: helpful when flipping variadic callbacks.
Overview
_.flip(func) returns a wrapper that invokes func with arguments reversed. It fits Lodash’s family of function decorators alongside curry and bind helpers.
Syntax
_.flip(func)- func: function to wrap.
- returns: new function that reverses arguments before calling
func.
Variadic arguments reversed
Classic lodash illustration: collect arguments after flipping so order mirrors the reversed parameter list.
import flip from "lodash/flip";
const flipped = flip(function () {
return Array.from(arguments);
});
flipped("a", "b", "c", "d");
// ["d", "c", "b", "a"]Binary subtraction
For two parameters, flip swaps operands—useful when an API passes arguments opposite to your helper.
import flip from "lodash/flip";
const subtract = (a, b) => a - b;
const flippedSubtract = flip(subtract);
subtract(10, 3);
// 7
flippedSubtract(10, 3); // calls subtract(3, 10)
// -7String concat order
Adapt a small formatter without rewriting its body—flip swaps which string lands first.
import flip from "lodash/flip";
const joinTwo = (first, second) => first + second + "!";
flip(joinTwo)("world", "hello ");
// "hello world!"📋 _.flip vs _.rearg vs array reverse
| Tool | What changes | Typical use |
|---|---|---|
_.flip | Every argument position mirrored left-right | Swap parameter order for binary or variadic functions |
_.rearg | Custom permutation you define | Pick arbitrary reorderings beyond full reversal |
_.reverse | Mutates an array’s elements | List ordering—not function signatures |
Pitfalls to avoid
Unary helpers
Flipping a single-parameter function is effectively a no-op aside from wrapper overhead.
One array argument
flip reverses discrete parameters; it does not reverse elements inside an array you pass as one value.
thisBound methods
Combine flip with bind carefully—caller context still flows through lodash wraps.
❓ FAQ
Summary
- Purpose:
_.flipwrapsfuncso arguments arrive reversed. - Contrast: prefer
_.reargwhen you need partial reorderings. - Next: Lodash _.memoize(), revisit Lodash _.delay(), or read official _.flip docs.
Lodash builds _.flip with createWrap(func, WRAP_FLIP_FLAG) (WRAP_FLIP_FLAG is 512), wiring argument reversal into the same meta wraps system used by curry and bind helpers.
6 people found this page helpful
