Accessing variables outside a function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Accessing variables outside a function

I know the way to access is by using return or console.log..... im not talking about those. I also know how js works in callstack. And function makes its own execution context and once its called it pops out of the callstack so the variable isn't accessible anymore. What i wonder is could ever be a way to access variable before even calling the function. Before it get in callstack. Like when getting the copy of whole function in memory component. https://code.sololearn.com/WJ63SfF30C18/?ref=app

27th Feb 2023, 6:03 AM
Javascript Developer
Javascript Developer - avatar
16 Answers
+ 3
No, in short, the variables in the function do not exist until the function is called.
27th Feb 2023, 6:18 AM
Chris Coder
Chris Coder - avatar
+ 3
Thanks PanicS I assumed Zeinab was talking about local scope since they mentioned how Js works in callstack. Typically, attempting to access a variable within a function prior to its invocation is not feasible, even if a version of the function is stored in memory. This is due to the fact that the variable exists within the function's local scope, which is generated and dismantled every time the function is executed. However, if the variable is defined within a global scope, it can be accessed from anywhere in the codebase. It should be noted that in this scenario, the variable is no longer exclusively associated with the function. Moreover, if you remove the var keyword from num inside the fun() function, it will create an "implicit global variable". This means that the variable will be created in the global scope, even though it was declared inside a function. Any subsequent reference to num inside or outside of the function will reference this global variable instead of a local variable.
27th Feb 2023, 8:21 AM
Chris Coder
Chris Coder - avatar
+ 3
Zeinab have a read about closures. " a closure gives you access to an outer function's scope from an inner function. In JavaScript, closures are created every time a function is created, at function creation time." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
27th Feb 2023, 4:51 PM
Chris Coder
Chris Coder - avatar
+ 2
There is way, just remove var/let/const, and variable will be moved to global scope. But in practice we avoid defining variables in global scope, because it can lead to data be changed somewhere else(creating hard bug to fix, when working on huge project), also it can create naming problem - already having variable with same name, conflicting to library variables, with variables from other peoples working on same project... Thats why even using var is bad, and many people now switch to let keyword. Here is more about defining variables inside function to global scope, with few ways of doing it: https://www.scaler.com/topics/javascript-global-variable/
27th Feb 2023, 7:06 AM
PanicS
PanicS - avatar
+ 2
If a function is supposed to carry and remember some data and exist beyond each separate call, wouldn't a class then make more sense? I mean, technically that's a data container with a "function" (method) that allows to access the data.
28th Feb 2023, 8:01 AM
HonFu
HonFu - avatar
+ 2
This code defines a function called fun which returns the value of a variable num that is set to 7. Then the function is called and the result is logged to the console using console.log(fun());. However, the second console.log(num) statement will throw an error because the num variable is only defined inside the fun function and is not accessible outside of it. So when you try to log num outside of the function, it will be undefined. To fix this error, you could define num outside of the fun function so that it is accessible from both inside and outside the function. <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <script> var num = 7; function fun(){ return num; } console.log(fun()); console.log(num); </script> </body> </html>
28th Feb 2023, 6:36 PM
k dot
k dot - avatar
+ 2
Basil you're right. instead of a static analysis of the code. if you don't have access to the source code, you'd probably need to hack into the system to get data. That's another can of worms. 😁 Anyway, thanks to this topic, at least it got me introduced to code 'crawling' as a concept similar to web crawling.
2nd Mar 2023, 12:25 AM
Bob_Li
Bob_Li - avatar
+ 1
yes i was wondering if a local variable inside a function could be accessed when the function is stored in memory component. Even before the function is called and make its own execution context. It couldn't be accecced at all because when the copy of whole function is stored in memory, before running the code, the variable isn't stored. Only a copy of whole function. So if we console.log the variable before or after calling the function it say (not defined) not (undefined). Local variables are trusable and safe Thanks for sharing your knowledge guys🙏🏽🙏🏽
27th Feb 2023, 9:08 AM
Javascript Developer
Javascript Developer - avatar
+ 1
I wd'like to Know if this app have the identicall funcion whit APP Bot our termux. I don't speaking Inglish... I'm bigner, but I say portuguuese, i need to samebody to help me but say this my language.
28th Feb 2023, 8:11 AM
James
James - avatar
+ 1
Zeinab interesting. if you could explore the AST of your code, it might be possible to get the local variable of any scope. type your code here: https://astexplorer.net maybe someone could examine the source code and write a parser that deconstructs the function to extract the local variables? I know that you could use. toString() to get a string version of the function definition. function foo(){ let n = 100; return 2*n; } let s = foo.toString(); console.log(s); //s will console log the function definition as string. How to process that string is the tricky part. writing parsers is beyond me, unfortunately. But this feels like a good thing to learn...😁 or maybe just do a search for 'let' or 'var' and output the substring after that would be the simpler approach?
28th Feb 2023, 12:07 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li wow intresting. Im goin to learn about it
28th Feb 2023, 1:32 PM
Javascript Developer
Javascript Developer - avatar
+ 1
deciphering strings is a hard task. That's why AI research and ChatGPT are getting so much $. if you get a working function of something like: let v = getLocalVarValues(func); I would like to see it.😁 functions and variables are written in so many ways. Then there's newline, tabs, etc. anyway, here is the link that got me to the AST parser rabbithole https://superdevelopment.com/2017/07/24/asts-and-javascript-write-code-that-writes-code/
28th Feb 2023, 1:46 PM
Bob_Li
Bob_Li - avatar
+ 1
Sourav Chettri thanks for you effort bro. Actually we didnt want to define it as a global variable. Just wondered if a local variable could be ever accessed outside of its scope. By knowing the fact that logicaly it is not possible
28th Feb 2023, 7:33 PM
Javascript Developer
Javascript Developer - avatar
+ 1
Zeinab experimenting with some ast parsing libraries. Here I'm using Acorn. similar libraries are Esprima and ESTree. Interesting how one could pick apart a function and what could be done with it. Not really practical and probably overkill, but it's a way to pick apart a code and get the values in it. https://code.sololearn.com/W18mqHEUk6AM/?ref=app
1st Mar 2023, 4:29 PM
Bob_Li
Bob_Li - avatar
0
PanicS thanks . Yeah I know about global scope and global variable... I was wondering if a local variable could be accessed in global scope. I knew it's not possible because it is not exict in global scope.but still was curious if it could be a tool ...😅
27th Feb 2023, 8:10 AM
Javascript Developer
Javascript Developer - avatar
0
Chris Coder yeah i know about closures and what makes it intresting is even after the outer function pops out of the callstack and everything about it is garbagecollected the inner function still remembers of that function because of the closures.
27th Feb 2023, 5:05 PM
Javascript Developer
Javascript Developer - avatar