DEV Community

Discussion on: Functional Programming: Refactoring Global Variables Out of Functions

Collapse
 
aderchox profile image
aderchox

Instead of doing this:

  let copyBooks = [...books] <--- we used a copy of the array in our functions.
  copyBooks.push(bookName);
  return copyBooks ;
Enter fullscreen mode Exit fullscreen mode

you could have also done this:

  return books.concat([bookName]);
Enter fullscreen mode Exit fullscreen mode

because the concat method doesn't change the existing array and returns a new array.