Need clarification about array mutation

Was a bit stuck on this one(largest numbers in arrays)

Research led me to a bunch of statements, that it’s better not to mutate array inside function, so I solved it like this:

function largestOfFour(arr) {
  const newarr = => {
    return Math.max(...elem);
  return newarr;

Any feedback about the above?

Regarding with your title, is there any reason you want to mutate the original array? If not, your code is fine. You can do it in one line, actually:

function largestOfFour(arr) {
  return => Math.max(...elem));
1 Like

I guess for solving this particular challenge it doesn’t matter much. Big thanks for syntax tip.

yup, if you feel like mutating X matters, feel free to do so :slight_smile:

The main thing about the original data is you might need it for other purposes, so mutating that data is defying your ability to do that anywhere else in the code.


Mutating objects (arrays are objects) inside functions can lead to unexpected side-effects. It is usually better to make the change to a copy and return that new state out of the function. At least then you know what the data is at the function call site because it is what was returned.

In the flow of a large scale app, side-effects (mutations on references) performed inside functions can be harder to reason about as opposed to just return values.