Refactor Global Variables Out of Functions quick question

What does let newArr = […arr] do exactly?

  **Your code so far**
// The global variable
const bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];

// Change code below this line

function add(arr,bookName) {
let newArr = [...arr]
newArr.push(bookName);
return newArr;

// Change code above this line
}

// Change code below this line
function remove(arr,bookName) {
let newArr = [...arr]
const book_index = bookList.indexOf(bookName);
if (book_index >= 0) {

  newArr.splice(book_index, 1);
  return newArr;

  // Change code above this line
  }
}
console.log(add(bookList,"The vikings of Valhala"))
console.log(remove(bookList,"On The Electrodynamics of Moving Bodies"))
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36

Challenge: Refactor Global Variables Out of Functions

Link to the challenge:

1 Like

I realized, I don’t really understand it because of your question. :rofl:

I used to pass in arguments to a function using it. Anyway here are links for more info, because I will misguide you if I try to answer

here is an article from the freeCodeCamp news:

The mozilla docs

let newArr = […arr] is just copying the array “arr” into newArr. “…” it is called spread operator. Since array is mutable i.e. if you made any changes in arr then it will directly made changes in newArr which may leads to problem. With the help of spread operator this will not happen.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.