Problem with a function call and a global variable

some Code

I tested this code on nodejs after I installed it on my computer.
now why every time I invoke the function test(), it gives 1 then 2 then 3 instead of the value of myArray2, even if I wrote return myArray2 in the function?
thank you.

If you look at the docs (which is always a good thing to do) you will see that the return value for push is the number of items in the array. So, you are changing the array, you just aren’t displaying it. If you type myArray there at the end, you should see it.

1 Like

is it the same thing for .unshift?

Yes, but at the risk of being snarky, you should get in the habit of checking yourself. Type “MDN unshift” into your search engine and see what the return value is.

I am constantly having to look these up. Do they alter the original? What do they return?

One of the most important skills a developer has is knowing how to look things up. No one can memorize all this stuff.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.