JS Built-ins
FunctionalProgrammingintermediate

Pure vs impure β€” array operations

πŸ“– Usage Example
// Pure: returns new array
const pureAdd = (arr, item) => [...arr, item];
// Impure: mutates original
const impureAdd = (arr, item) => { arr.push(item); };

Pure functions avoid mutating arguments β€” return new values instead.

Distinguishing Pure and Impure Functions

Learn to identify pure functions by checking whether they modify their inputs or depend on external state.

Challenge: Create a pure version of array transformation. Given an array, return a new array with each element doubled without modifying the original.

πŸ’‘ Hints (3)
  • β€’ Use .map() to create a new array
  • β€’ Never modify the input array
  • β€’ Pure functions should not have side effects