Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Head and Tail

Data Structures, Operators & Stringseasy

Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 111: The Spread Operator (...)

Implement `headTail(arr)` returning `{ head, tail }` where `head` is the first element and `tail` is an array of the rest, using array destructuring with a rest element.

Sample tests

Input: headTail([1,2,3])
Output: {"head":1,"tail":[2,3]}
Input: headTail([5])
Output: {"head":5,"tail":[]}

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • On an empty array head is undefined; the tests here use non-empty inputs.

Learning resources

  • MDN: Destructuring assignment
Visualize this concept: Spread and Rest →
Approach & explanation (try first)

Destructuring with a rest element captures the first item and gathers the remainder into a new array.

Loading...
⌘/Ctrl + Enter

Run your code to see results.