JS Global and Local | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

JS Global and Local

I was creating a game when I realised I had an issue. My function (called init) in which the variable ctx was stated couldn't be accessed by functions outside of it. So I decided to put the functions in init, but that didn't work. Now I don't know how to have things drawn in my code. I figured this has something to do with global and local scopes but I have little understanding of this. Could anyone help

30th Dec 2018, 12:39 AM
Zakariya
Zakariya - avatar
1 Answer
+ 2
You can probably instantiate your ctx variable on a globally scope which mean outside of any particular function, so all those functions can acess it as long as your code goes. var ctx = value; function init (){ // code }
30th Dec 2018, 1:10 AM
Zenobio
Zenobio - avatar