0

I am new to javascript. Coming from a more object oriented background, one quirk I noticed is that some methods override an array, and some others make new instances. For example:

// initialize array
var arr = [
  "Hi",
  "Hello",
  "Bonjour"
];

// append new value to the original array
arr.push("Hola");

console.log(arr);

whereas concat

var arr = [
  "apple",
  "banana",
  "cherry"
];

arr = arr.concat([ // original array needs to be overridden
  "dragonfruit",
  "elderberry",
  "fig"
]);

console.log(arr);

Why is the behavior between the two methods not consistent?

Examples taken from: How to append something to an array?

John-Henry
  • 1,556
  • 8
  • 20
  • 1
    depends on the internal algorithm used is in-place or not. – Code Maniac Apr 09 '20 at 11:20
  • 2
    Because some methods are meant to update current array and others are not. Functions like `concat`, `map`, `filter` are intended to return a new array with some permutation. `push`, `pop`, `unshift` `shift` are meant to mutate the same array – Rajesh Apr 09 '20 at 11:20
  • 1
    Just to make sure - are you asking why back in the 90's - the creators of Javascript thought it would be best to write it this way? – Dekel Apr 09 '20 at 11:20
  • @Dekel I don't know what answer I am looking for -- is this just a historic quirk? Are "modern" methods leaning in a certain direction? – John-Henry Apr 09 '20 at 11:23
  • 1
    Have a look here to see which Array methods are mutable and which ones are not - https://doesitmutate.xyz, but basically the concepts here that you'd want to look into more to answer your question is `mutability` and `immutability` – goto Apr 09 '20 at 11:24
  • 1
    @John-Henry certain use cases need certain capabilities, for example you want something to change in your original array you will use any of method which mutates original array ( less preferred though ) or other hand you don't wan to mutate your original array you'll use other methods – Code Maniac Apr 09 '20 at 11:26
  • 1
    @John-Henry this has nothing to do with "historic quirks". Concepts of `mutability` and `immutability` exist in other languages as well, not just `JavaScript` - there's a reason why they exist so I'd suggest you look into those concepts more to answer your question. – goto Apr 09 '20 at 11:28
  • @CodeManiac I see -- it doesn't seem like every method has a corresponding counterpart though, is that correct? – John-Henry Apr 09 '20 at 11:28
  • Thanks @goto1 I was asking that in regard to that other post, but I will certainly look into those concepts – John-Henry Apr 09 '20 at 11:29

2 Answers2

1
var foo = 1, bar = 2;

Let's say you wanted to add the values foo and 1 onto bar. This is what you would do:

var foo = 1, bar = 2;
bar = bar + foo + 1;
console.log(bar) //4

But you did not do this:

var foo = 1, bar = 2;
bar + foo + 1; //ineffective
console.log(bar) //2

That is because the operator + simply just adds the values and returns the sum. So you still need to redefine bar. Whereas in:

var foo = 1, bar = 2,i;
for(i=0;i<foo;i++) bar++; //adds 1 foo times
bar++; //adds 1

You do not need to make a redefinition. This is because the increment operator assumes that you want to increment a value, not find their sum. So it does that.


array.concat(foobar) assumes that you want to get two arrays or values, join them together into a new, combined array, and then return the new array. In a sense, it is like the + operator. Thus it only finds their combination, not set the actual array, so you need a redefinition.

Whereas array.push(foobar) assumes that you want to add, stick, glue, or push a value to the end of the array, so it will do so. And because it itself has already added those onto the array, it doesn't need to be used in a redefinition.

They are used in that sense. It may be inconsistent, but you need to become used to it.

Kino Bacaltos
  • 373
  • 1
  • 2
  • 16
0

concat() method does not change the existing array, but returns a new reference.