1

I've just read the answers for this question about 'setting global variables inside a function' but I still have a doubt.

This maybe is very basic but can someone tell me why an error does not occur when I do this? I understand that what is passed to the function is a copy of the value of the variable i, but... why

var i = 5;

function exp(i) {
    i = 7;
    console.log(i);
}

exp(i);
//logs 7

or

function exp(i) {
  return i = 7;
  console.log(i);
}
exp(5)
//logs 7

and:

function exp() {
  return 5 = 7; //or console.log(5 = 7);
}
exp()//Uncaught ReferenceError: Invalid left-hand side in assignment

In the first example am I not making 5 = 7? Why the function logs '7'?

This all came up after I've seen this example in the wonderful JavaScript Garden about local variables:

// global scope
var foo = 1;
var bar = 2;
var i = 2;

function test(i) {
    // local scope of the function test
    i = 5;

    var foo = 3;
    bar = 4;
}
test(10);

Why test(10) which sets 10 = 5 inside the function does not make an error?

Community
  • 1
  • 1
viery365
  • 935
  • 2
  • 12
  • 30
  • Your are not making 5=7, your setting value 7 to variabile i. If you do "return i==7" then it returns 5==7, hence false. – Franz Tesca Aug 06 '16 at 20:46
  • But if that is the case I would be changing the global variable i to 7 and that is not the case. the variable i will still be 5 (in the first example) – viery365 Aug 06 '16 at 20:47
  • Possible duplicate of [Is JavaScript a pass-by-reference or pass-by-value language?](http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – 4castle Aug 06 '16 at 20:52
  • In the first example you set i=7 and in fact console.log(i) will output 7... – Franz Tesca Aug 06 '16 at 21:00
  • Are you familiar with variables and assignment statements? `i = i + 1` is a valid statement that increments `i` by `1`. `i = 7` changes `i` to `7` and forgets the previous value. – 4castle Aug 06 '16 at 21:10

2 Answers2

2

You can't do 5 = 7. 5 will always be 5.

i = 7 equals to set var i as 7

1

your first method

var i = 5;

function exp(i) {
    i = 7;
    console.log(i);//of course it prints 7, i belong to exp scope
}
console.log(i)//it will print five
exp(i);

you are not giving new value to i variable but just to parameter you got

function exp() {
  return 5 = 7; //or console.log(5 = 7);
}

your trying to returnn 5 = 7; what's 5? In Javascript if you are not using "strict mode" then you can declare variable without var keyword, but variable name cannot start with number.

function exp() {
      return 5 + 7; //or console.log(5 + 7);//this will work
 }
 function exp(i) {
      i = 5
     return i = 7; //or console.log(5 + 7);//this will work
 }
Jorawar Singh
  • 7,463
  • 4
  • 26
  • 39
  • I think I understand now:) Thank you:) So, the argument of a function call gives always a value to the parameter of that function, and that parameter has to be a value, never a variable name. That's why in functions constructors function like `function X(name){this.name = name}`the parameter 'name' refers to the value `name`and never to this.`name`. Now I think I understand finally:) – viery365 Aug 06 '16 at 21:53