+ 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?

26th Feb 2019, 7:28 AM
Aakaanksha 💕 [TheBraveCoders]
Aakaanksha 💕 [TheBraveCoders] - avatar
17 Réponses
+ 28
KrOW, sorry. i have edited the question what about now? Thanks Serena Yvonne and KrOW i understood now 🤗
26th Feb 2019, 8:25 AM
Aakaanksha 💕 [TheBraveCoders]
Aakaanksha 💕 [TheBraveCoders] - avatar
+ 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.
26th Feb 2019, 8:53 AM
Arushi Singhania
Arushi Singhania - avatar
+ 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
26th Feb 2019, 9:05 AM
KrOW
KrOW - avatar
+ 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)
26th Feb 2019, 8:01 AM
KrOW
KrOW - avatar
+ 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!
27th Feb 2019, 4:49 PM
Benneth Yankey
Benneth Yankey - avatar
+ 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.
26th Feb 2019, 8:10 AM
Arushi Singhania
Arushi Singhania - avatar
+ 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.
26th Feb 2019, 7:34 PM
Rick Shiffman
Rick Shiffman - avatar
+ 2
A and B is local to myfunc And c and d are global for myfunc and vice versa
26th Feb 2019, 6:54 PM
AKS
AKS - avatar
+ 2
all the variables are local, because they are described and declared inside the functions
28th Feb 2019, 9:03 AM
Luka Timofeev
Luka Timofeev - avatar
+ 1
return statement must bring them back to the to the main scope. am i right?
27th Feb 2019, 4:05 PM
Ahmad Ali
Ahmad Ali - avatar
+ 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.).
27th Feb 2019, 5:45 PM
Benson Saju
Benson Saju - avatar
+ 1
local
28th Feb 2019, 4:19 AM
Ashikul Islam
Ashikul Islam - avatar
+ 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
4th Mar 2019, 9:36 AM
Meghrajsinh Solanki
Meghrajsinh Solanki - avatar
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.
12th Mar 2019, 7:19 PM
Jared Hutchison
Jared Hutchison - avatar
0
All the variables a local as they are declared inside a function
21st Jul 2020, 8:28 AM
Kittu
Kittu - avatar
- 3
Hi
27th Feb 2019, 12:04 PM
Piraveen Kuddy
Piraveen Kuddy - avatar