Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: is it safe to assign the result of array.filter() to the same array

Tags:

javascript

Is it officially safe to:

someArray = someArray.filter(function(item) {
              return item !== 'something';
            });

I mean assigning back to the same array the filter function is called for, in one line. And same question for all other array prototype functions that return an array.

like image 657
matanster Avatar asked May 25 '26 17:05

matanster


1 Answers

Yes, it's safe because it's returning the value from the function.

You should be careful about making sure you're reusing it correctly, though.

like image 128
Codeman Avatar answered May 28 '26 06:05

Codeman