Every React codebase uses classnames or clsx. Rebuilding it shows you can handle a polymorphic argument list cleanly.
Each argument can be one of several shapes. Branch by type:
Falsy arguments (false, 0, '', null, undefined) are silently dropped, which is what makes classNames('btn', isActive && 'active') read so well.
.filter(Boolean).join(' ')?" → that misses the object and array forms that make the API useful.Set if asked, noting the perf cost.Implement it, then handle deeply nested arrays of objects in one pass.
function classNames(...args) { // your code here } console.log(classNames('btn', { active: true, disabled: false }, ['lg', 'rounded'])); // "btn active lg rounded"
Test Code
Enter JavaScript that runs after your solution. It should return a value or a Promise.