// the global variable
var bookList = ["The Hound of the Baskervilles", "On The Electrodynamics of Moving Bodies", "Philosophiæ Naturalis Principia Mathematica", "Disquisitiones Arithmeticae"];
/* This function should add a book to the list and return the list */
// New parameters should come before the bookName one
// Add your code below this line
function add (bookList, bookName) {
let bookListInsideFunction = new Array();
bookListInsideFunction = bookList;
bookListInsideFunction.push(bookName);
return bookListInsideFunction;
// Add your code above this line
}
When I call function add, it’s observed that even global variable bookList value is getting updated. Even when I’m doing action on local variable only.
It is because this doesn’t make a copy, just a new reference
To copy an array you need to use slice(), concat() or the spread operator
You also don’t need the line where you create new Array(), you can just initialise a variable with value of copy of the array you want to copy