+ 59
Variable Scope Confusion 😵
if i have a function like this: function myFunc(a, b) { return a+b; } My question is, Variable a, b is local or global? function myFunc2() { c=8; d=1; return c+d; } what about c and d?
16 ответов
+ 28
KrOW, sorry.
i have edited the question what about now?
Thanks Serena Yvonne and KrOW
i understood now 🤗
+ 13
Serena Yvonne
You are right in that undeclared variable is global, the reason is that they will be added as a property to window. See Morpheus demo below for detailed explanation.
https://code.sololearn.com/WwhKQUoMbXHH/?ref=app
Nevertheless, it's not a good habit to use variable without declaration.
+ 8
Scope works differently if you use var/let/const or nothing. If you declare a variable withouth using keyword, it will be in global scope without taking cure where you declared it.
function f1(){
function inner(){
imGlobal= true
}
inner();
}
f1();
console.log(imGlobal);//true
If you use var, you var will have like scope the entire encosling function if its declared inside a function, or global if its not.
function f2(){
// Note this block dont prevent imLocal to be
// putted inside f2 scope
{
var imLocal= 'local';
}
function inner(){
imLocal= 'local-inner';
}
inner();
console.log(imLocal); //local-inner
}
f2();
If you use let keyword, then your var will be block-scoped (or global if its declared outside any block). Repeating same example for var but using let, you will have:
- imLocal in inner will create a global var
- log imLocal inside f2 function will print imLocal (than in reality is global) declared by inner function because the imLocal inside f2 is not visible anymore because its block-sopec (remember the block)
function f2(){
// Note this block prevent imLocal to be
// putted inside f2 scope NOW respect var example
{
let imLocal= 'local';
}
function inner(){
imLocal= 'local-inner';
}
inner();
console.log(imLocal); //local-inner
}
f2();
Using const is similar to let but it dont allow redefinitions of variable in same scope
+ 7
a and b are locals to function myFunc.
The second function is not even executable because it raise a parsing error (var in arguments)
+ 6
A lot has already been said, however I suggest you read my blog post on variable declarations in javascript to get more insight.
https://code.sololearn.com/WdauYjg8rOiF/?ref=app
Hope it helps, happy coding!
+ 5
In general, scope is area between a pair of curly braces. Variables belong to the scope in which they are declared. Btw KrOW has answered your question.
+ 5
a and b are local to the function myFunc, and the value of a+b is returned.
a and b are the parameters of myFunc, or place holder for the values you pass to myFunc.
In myFunc2 , c and d are global to it, they must be defined in an outter block of code. myFunc2 changes c to 8 and d to 1 for the whole world. Oh, myFunc2 always returns 9.
In javascript undeclared variables default to program wide globals.
+ 2
A and B is local to myfunc
And c and d are global for myfunc and vice versa
+ 2
all the variables are local, because they are described and declared inside the functions
+ 1
return statement must bring them back to the to the main scope. am i right?
+ 1
The variables a and b are local to the function MyFunc.and also c and d are local variables to the function myFunc2.(NOTE:In the second function declare the variable with datatypes.).
+ 1
local
+ 1
a global var is created outside of function, it will be accessible anywhere in doc. and a local var which created in fun. , it will work only in that function
0
In order to declare a global variable, you must declare it outside all functions. All variables declared inside of a function's opening and closing braces arocal variables to that function.
0
All the variables a local as they are declared inside a function
- 3
Hi