This question already has answers here:
What is the different between a parameter and a local variable?
(4 answers)
Closed 6 years ago.
When working with functions in JavaScript, are variables declared as parameters in a function different from variables declared within the function itself?
For example, is
function functionName (var1, var2)
different to
Function (var1, var2) {
Var VarName;
}
In the second example, could var1 be declared where varName is declared? If not, why?
var1 and var2 are parameter variables, they automatically get their values from the arguments provided when the function is called.
Variables declared with var inside the function body are local variables of the function.
Parameter variables are automatically made local to the function. You can have
var var1;
in the function, but it's redundant and has no effect. It doesn't create a new variable or override the value of the parameter (unless you include an initializer).
function myFunc(var1, var2) {
var var1;
var var2 = 3;
var newVar;
console.log(var1, var2, newVar);
}
myFunc(1, 2);
Related
This question already has answers here:
How do JavaScript closures work?
(86 answers)
Closed 3 years ago.
I'm confused as to why the below will output "1".
If I use var to declare a in myFunc that should limit the scope of a to that function.
Calling "a" in the anonymous function (in the setInterval call) is a different function so why is that considered with the scope of myFunc?
Is it only because the function is anonymous? I would expect a to be available to another function unless bind() was used.
myFunc = function(){
var a = 1;
var int = setInterval(function () {
console.log(a);
}, 5);
}
The function bound to setInterval is a closure, when something is unknown in its own scope, it looks in the outer scope, in your case myFunc scope
This question already has answers here:
How do JavaScript closures work?
(86 answers)
How is a closure different from a callback?
(9 answers)
Closed 5 years ago.
From every definition I have looked up, closure is when a function is created or declared from within another function. Examples are plentiful across blogs and websites of this happening. But what about when the function is declared outside of another function, but called then called from within a function? For example:
const add = (x,y) => {
return x + y;
};
const double = num => {
return add(num,num)
};
let a = double(6);/*?*/
Does add(num, num) create closure? If so, please help me understand why.
Closures are a product of lexical scope.
A closure is the combination of a function and the lexical environment
within which that function was declared.
You must define a function within another one to create a closure.
What you do in your example is simply call a function that calls another and uses its return value. At no point can the double function access the scope of the add function: their scopes are distinct.
Here is a classical example of JavaScript closure
var makeAdder = function(x) {
return function(y) {
return x + y;
};
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);
console.log(add5(2)); // 7
console.log(add10(2)); // 12
In both cases, the functions returned by makeAdder, "remember" the outer scope in which the were created (the scope created by the makeAdder function).
Anything that was in scope of the outer function was also in scope for the inner function, and it remains so when the inner function is returned.
This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 5 years ago.
I tried to search it on internet but didn't got any answer. Why access of two variables before declaration have two different outputs:
function test() {
console.log(a); // prints 'undefined'.
console.log(b); // prints 'b is not defined'.
var a = 5;
this.b = 10;
}
when you declare var variable inside the function then that variable is limited to that function. That mean scope of that variable is limited to that function so you can't access that variable outside of that function.
But if you use this then it can be access through outside but then again you need to create a object of that class/function and access trough it.
function test() {
var a = 5;
this.b = 10;
this.printB = function(){
return this.b;
}
this.printA = function(){
return a;
}
}
var obj = new test();
console.log(obj.a)
console.log(obj.b)
console.log(obj.printA())
console.log(obj.printB())
when you write a variable with var like var a it become a function
level variable. so now a is a variable which is present there before consol.log(a) and its not given a value so its undefined for b there is no variable defined with name b before it called.
And which all variables are defined with var will be function level variable and will be created before anything else in the function. so a will be present in the function at the starting itself.
and here this will be holding the object of Windows.
First of all, you are trying to examine the variables before you declared them.
This is the correct order:
function test()
{
var a=5;
this.b=10;
console.log(a); // Should print 5
console.log(b); // Should print undefined
console.log(this.b); // Should print 10
}
Second, regarding your question, declaring a var inside your function, make is a local private variable inside your function.
declaring this.b, will cause b to be in a different scope, and b for itself as undefined. If you want to use b inside the function, use this.b.
This question already has answers here:
Access overridden global variable inside a function
(2 answers)
Closed 6 years ago.
How to access global variable from a function when the name is same as argument name in JavaScript ?
var name = null;
function func(name) {
// How to set the global variable with the passed value
}
Change the name of the parameter to something else. No other way, because it will always see the innermost name.
var name = 'Name outer';
function func(name) {
console.log(name);
console.log(window.name);
}
func ('Name inner');
However, this would be bad practice and you should avoid having this situations.
If you are really talking about a global variable this can be done this way:
function func(name) {
window.name=name;
}
in a browser or
function func(name) {
global.name=name;
}
in node.js but if you declared name within a function there is afaik no way to do that.
However, you should avoid gobal variables if possible because they are shared by all used code including libraries and you can't know if this has any side effects in case of a name colision.
This question already has answers here:
Difference between this and var in a function
(5 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 9 years ago.
I'm new to JS so noob question:
I see functions which define vars inside them:
function functionName(){
this.something = "";
};
If I understand correctly, something is a local variable? Why is it being defined as this.something = '' as opposed to var something = ''?
Is there any difference, and if so what is it?
It sets the attribute something of this. What this refers to depends on how functionName is invoked, but typically it's an object created with new:
var foo = new functionName();
console.log(foo.something);
If you'd use var something inside the function, the variable would be inaccessible from outside, so you couldn't do foo.something in the above example.
var someThing
is a local variable - meaning it exists within the scope of the current block
this.someThing
is an instance variable - meaning it belongs to the object and is visible in all methods of that object.