-1

I am a new learner, learning to code front end development. I want to know about the global and local variables in JavaScript functions. I understand the local and global, but how about the function parameters, are they local or global?

Should I always define a function as function add(a,b) or function add(var a, var b)?

Somewhere I read that any variables defined without the var keyword inside a function becomes global. This point is confusing me.

3
  • 3
    function add(var a, var b){} is not correct syntax, it is always function add(a, b){}. Commented Dec 22, 2016 at 12:54
  • 1
    Where did you see parameters with var keyword? This is not an allowed syntax. Function parameters are limited to the function's scope Commented Dec 22, 2016 at 12:54
  • Thanks for making it clear. Now I will always keep this in mind that var keywords are not allowed within the brackets. Commented Dec 22, 2016 at 13:10

4 Answers 4

1

The first one. function add(a,b)

Using the var keyword up there is actually a syntax error.

somewhere I read that any variables defined without the var keyword inside a function becomes global

That's true, but only when you do c = 'something'. Function parameters are always locally scoped.

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

1 Comment

Thanks for your help, It became clear.. I was confusing with the C++ syntax.
1

The parameters are local to the function, but this is only half the truth, since objects are passed by reference. So if your function takes an object and modifies any member of this object, then the change is also seen outside of the function (because it's the same object).

Try the following in a node shell to demonstrate:

var obj = { a:0 };
obj;
function f(o) { o.a++; }
f(obj);
obj;

$ node

var obj = { a:0 };

undefined

obj;

{ a: 0 }

function f(o) { o.a++};

undefined

f(obj);

undefined

obj;

{ a: 1 }

Comments

0

In function add(a,b) a and b are only accessible within the function so they are local to that function.

function add(var a, var b) is incorrect syntax. You can't put var there.

Comments

0

You must declare a function such as function add(a, b). a and b are the parameters you'll give to the function when you will call it.

They don't exist at the moment of declaration, and are local to the variable. They are more or less a reference to the variables you'll pass to your function when calling it.

var num1 = 4;
var num2 = 3;

function add(a, b){
   return a + b;
}

var result = add(num1, num2);

// result : 7

Variables declared inside a bracket's function without the keyword var become global, such as result in the following example :

function add(a, b){
  result = a + b ;
  }

Comments

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.