1

I'm new to programming and am unable to find any good explanation on parameter/arguments, how they work under the hood. For eg:

function changeStuff(a) {
        return a = a * 10;
      }

var num = 10;
console.log(changeStuff(num)); //prints 100
console.log(num); //prints 10

When I call this changeStuff function, how does javascript put variable num into parameter - a? Does it do something like a = num under the hood?

I'm sorry if its a bad or a dumb question.

9
  • The argument a will have the value of num which is 10. so the function will calculate 10 * 10 and returns the result. Commented Nov 13, 2018 at 15:30
  • 2
    you would need to see compiled code rather than this interpreted code to understand it more, as you don´t even deal with registers here. If it works same way as c++ (probably similar), when calling a function, function parameters are pushed onto current stack. Then the function reads values from the stack according to the amount and function reads/writes those values Commented Nov 13, 2018 at 15:32
  • 2
    Possible duplicate of stackoverflow.com/questions/13104494/… Commented Nov 13, 2018 at 15:33
  • @juvian that should be an answer. Great explanation. Commented Nov 13, 2018 at 15:33
  • @Thielicious, so inside javascript engine it is saying num = a? It is just abstracted away from us? Commented Nov 13, 2018 at 15:34

2 Answers 2

3

You would need to see compiled code rather than this interpreted code to understand it more, as you don´t even deal with registers here.

Assuming it works same way as c++ (probably similar), when calling a function, function parameters are pushed onto current stack. Then the function reads values from the stack according to the amount and function reads/writes those values.

In compiled code, there will not exist such thing as 'a' variable. Only a limited amount of registers are available, so a will actually be one of those. Before assigning that, the value from the register will be pushed onto the stack, so that when function ends, the register can go back to its previous value for the running code that might have been using it before calling the function.

A bit of literature on the subject

Sign up to request clarification or add additional context in comments.

Comments

0

Javascript copies the reference to the function a * 10 to the variable a in this case. So a is a * 10 then and so a * 10 will evaluated and returned.

3 Comments

Except 10 is a primitive value and those are passed by value in javascript; if they were passed by reference as you say the num var would be modified by the function.
a * 10 is a function. a will get a reference to the function a * 10. a function is an (complex) object. If you have a complex type, even if it contains primitive types, it will be passed by reference. num will be passed to a in the function a * 10 as value, a in the return statement will get the reference to the function.
thanks, i guess i have to look up pass by value/reference thoroughly, maybe that will help me understand what going on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.