Why does returning an updated array, after a push() call, return a numeric value instead of the array contents?

Tell us what’s happening:

I tried the following. And the console printed out a 4 instead of the names ( including Pete ).


function addFriend(userObj, friend) {
  // change code below this line  

  return userObj.data.friends.push(friend);

  // change code above this line
}

console.log(addFriend(user, 'Pete'));

If I separate things like -

function addFriend(userObj, friend) {
  // change code below this line  

  userObj.data.friends.push(friend);
  return userObj.data.friends;

  // change code above this line
}

The above, returns the array contents as expected. What’s happening under the hood to cause the differences in what console.log prints out?

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/modify-an-array-stored-in-an-object/

As per https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push#Adding_elements_to_an_array

this seems something inherent to push() itself. Not sure why this would be something useful, though. But I guess it’s just how it’s implemented? (or should I try to dig deeper here?)

By your link:

Returns the new length property of the object upon which the method was called.

You’re right. It doesn’t make too much sense to return that, javascript was made in a hurry, so there’s a lot of bad/weird parts. They can’t just change it though, image how many codes may depend on that.

1 Like

It’s not wierd or bad behaviour, this is absolutely expected, and how it works in a very large number of languages. It needs to work that way because it is how you implement stacks and queues.

Really? Javascript is the only language (from those that I know) that returns the length of the new array.

Some quick search:

  • java - The method call returns the item argument.
  • python (append) - It doesn’t return any value.
  • C# - void
  • dart (add) - void

I’d like to know what you mean by “a very large number of languages”?

No worries, I still love my javascript even with its shortcomings :heart_eyes:

How is returning the length of the array helpful to implementing stacks and queues?

And is that something we/I will get to learn in any detail through this JS course/curriculum? I am not feeling confident about what I am learning here compared to what I have heard software developers learn and work upon. I was hoping what I learn here for DS/Algo would transfer to other languages too, but feeling less confident about that now :frowning: But I guess will keep going through this and keep learning.

In Java it does, dequeues and linkedlists and stacks. In Python it does, dequeues. C# same. C++ same. Etc etc. Almost all languages with a good selection of data structures out of the box implement it on the relevant data structure.

Arrays in JS are not like arrays in many other languages, they are just a variation in the only data structure available in the language. There is no dequeue data structure or stack data structure available out of the box, so everything is dumped on array. So same as Perl, which is similar to JS in that respect (where JS lent this from afaik). Ruby is same as Perl. Both of those have push/pop etc on arrays.

Not really, this isn’t a CS course. But conversely, if you do a CS course, then you are unlikely to be learning much of what a software developer actually does day-to-day most of the time. The knowledge is fully transferrable. There is just a great deal of it, and FCC, as with any course, can only provide a bit of that knowledge: the course is big and covers a lot of ground, but it’s still just one course, focussed toward web development.

(Sorry wasn’t trying to avoid the first question, just on v low battery so can’t type up an example cos phone’s almost dead)