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?

5
  • 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. Commented Aug 6, 2016 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) Commented Aug 6, 2016 at 20:47
  • Possible duplicate of Is JavaScript a pass-by-reference or pass-by-value language? Commented Aug 6, 2016 at 20:52
  • In the first example you set i=7 and in fact console.log(i) will output 7... Commented Aug 6, 2016 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. Commented Aug 6, 2016 at 21:10

2 Answers 2

2

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

i = 7 equals to set var i as 7

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

Comments

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
 }

1 Comment

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 nameand never to this.name. Now I think I understand finally:)

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.