+ 2

How to access a variable inside setInterval function in global

Trying to access the number variable outside the function and i get error,help me fix want to console.log those random numbers as they change code: setInterval(function(){ number = Math.floor((Math.random()*10)); }, 1000); console.log(number); https://code.sololearn.com/WyD6tnhAKXjX/?ref=app

30th Nov 2021, 6:57 PM
K4MOGELO SITHOLE
K4MOGELO SITHOLE - avatar
21 Answers
+ 4
You cannot access local variable globally. Why you need to access outside the function?
30th Nov 2021, 7:03 PM
A͢J
A͢J - avatar
+ 1
J4D3N setInterval(function(){ window.number = Math.floor((Math.random()*10)); }, 1000);
30th Nov 2021, 7:16 PM
Artur
Artur - avatar
+ 1
But it's bad practice. There absolutely no reason to do that. And I've never met a single case when I needed to do that.
30th Nov 2021, 7:19 PM
Artur
Artur - avatar
+ 1
J4D3N Not in console. You can define 'window.number' variable inside your function and it's the same if you define 'number' variable in your main code flow aka global. Then you can access that variable. But if it is a local variable inside a function you can not access it from outer scope. And the only way (I know) to do that is as I did in the code above.
30th Nov 2021, 7:25 PM
Artur
Artur - avatar
+ 1
But remember that such properties are not variables! So function.number = 0 //property let number = 0//variable These too things are completely different.
30th Nov 2021, 7:29 PM
Artur
Artur - avatar
+ 1
I rewrote your code a bit. Just to give some food for thought. Notice how I declared only 1 global scope variable "start", and everything else is processed by functions. You may ask: "Why it is important?". Well, It is important because you don't pollute the global scope, and doing so you will avoid many mistakes in future. I used some advanced stuff here tho, such as ES6 destructuring, closure. But the code is really simple and hopefully, you'll get the idea. https://code.sololearn.com/Wg6pwOgt3kJk/?ref=app
30th Nov 2021, 11:59 PM
Artur
Artur - avatar
+ 1
السلام عليكم ورحمة الله
1st Dec 2021, 8:01 AM
عباس زكريا اسحق
عباس زكريا اسحق - avatar
+ 1
عباس زكريا اسحق وعليكم السلام ورحمة الله وبركاته
1st Dec 2021, 10:41 AM
Artur
Artur - avatar
0
Just define that variable in global scope. Or you could define it internally as 'window.number = ...'
30th Nov 2021, 7:03 PM
Artur
Artur - avatar
0
But if you really want to access a variable that is local to function then you need to add property to that function
30th Nov 2021, 7:17 PM
Artur
Artur - avatar
0
and it works,thanks😂🔥🔥🔥
30th Nov 2021, 7:36 PM
K4MOGELO SITHOLE
K4MOGELO SITHOLE - avatar
0
Artur Thanks This is what i was busy with https://code.sololearn.com/WtLHS4tqcMv1/?ref=app
30th Nov 2021, 8:46 PM
K4MOGELO SITHOLE
K4MOGELO SITHOLE - avatar
0
tried,got an error 😂so agg if it runs, don't touch but shoot,show me what u mean
30th Nov 2021, 9:40 PM
K4MOGELO SITHOLE
K4MOGELO SITHOLE - avatar
0
And let me clarify one point. These two following code snippets are essentially the same, but with one caveat: 1) function() { window.number = 5; } console.log(window.number) // 5 2) let number; function() { number = 5; } console.log(window.number) // undefined A variable declared with let/const doesn't become a property of the global object, which means it doesn't pollute the global object whereas window.number directly adds a property to global object.
1st Dec 2021, 12:10 AM
Artur
Artur - avatar
0
شكرا لك
1st Dec 2021, 5:34 PM
K4MOGELO SITHOLE
K4MOGELO SITHOLE - avatar
0
Is this app is enough to learn coding because i am new to this
2nd Dec 2021, 1:01 PM
Manoj.V
Manoj.V - avatar
0
Manojtanu Tanujanu Well it's a great App to start learning basics. and u can learn more advance stuff on YouTube maybe but ye Its a great app to learn Mate,Just don't limit ur self Try other apps and vids too
2nd Dec 2021, 1:04 PM
K4MOGELO SITHOLE
K4MOGELO SITHOLE - avatar
0
Thanks
2nd Dec 2021, 1:04 PM
Manoj.V
Manoj.V - avatar
0
I am a commerce student without computer science
2nd Dec 2021, 1:05 PM
Manoj.V
Manoj.V - avatar