Global variable! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Global variable!

How to make a variable global??

8th Aug 2021, 6:40 AM
Parth
Parth - avatar
3 Answers
+ 4
If I am not wrong you mean," can I use a var that is inside a function and call it outside a function?" I don't think you can. Because of how javascript, and most languages, scope variables, you can't access variables declared inside a function from outside a function. The variable belongs to the function's scope only, not the global scope. Reference: https://stackoverflow.com/questions/39679212/access-a-variable-inside-a-function-which-is-inside-a-function-in-javascript/46925605 Btw, you don't even need that. If you declare it globally(at the beginning of your code), you can access it from a function and change it and also call outside the function.
8th Aug 2021, 10:31 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 5
In js, A variable is by default global so you can call them from function: https://code.sololearn.com/W278oh06sf85
8th Aug 2021, 6:49 AM
The future is now thanks to science
The future is now thanks to science - avatar
+ 4
Ok, but can I use a var that is inside a function, outside a function??
8th Aug 2021, 7:54 AM
Parth
Parth - avatar