Isn't it wrong or just I don't know something

written in one of the books I am reading for my exam. stating that the global variable is copied to the function, parameter…

can someone clarify the statement

In CS:

  • argument : the actual value/expression passed as input into a function, routine or subroutine
  • parameter: (in most cases) is a local isolated copy of the argument passed to said function, routine or subroutine.

I’ve seen that book is in C, but it’s the same in JS.
So:

// defining a function

/**
* @param y -> is the local reference to whatever input we pass in
*/
function f(y) {...}

// when called
var x = 5;
f(x) // the argument supplied is whatever value x is holding
f('test') // the argument this time is the string 'test'

This means that a function can change whatever argument it receive without changing the original value

var x = 5;
function plusFive(n) {
  n = n + 5;
  console.log('x is ', x);
  console.log('n is ', n);
}

plusFive(x)
// x is  5 n is  10

x is unchanged as expected.

Hope it helps :+1:

1 Like