+ 21

[solved]Why I cant use a variable outside the function defined inside a function?

why I can't use a variable outside the function when defined inside a function .. . I made a variable var r=Math.random(); inside a function so that each time the function is called I will get a new random no. but I am not able to use it outside the function to verify other values. any solution

31st Jan 2017, 9:20 AM
Utkαrsh
Utkαrsh - avatar
58 Answers
+ 69
The reason you can not use the variable outside of the function is because of the variable scope, meaning if it is declared locally inside the function that is the only place you can use it. There are a couple of solutions depending on what you are trying to do accomplish. If you want to use r anywhere you can declare r globally and then just reassign the value of r inside your function. then you can check r after the function call. Another way is to return r and assign it outside of your function. Examples: Global Method: var r; function myFunction() { r = Math.random(); } this would leave r accessible everywhere for better or for worse. the method to return r for later use or verification would be: function myFunction() { var r = Math.random(); return r; } var valueOfR = myFunction(); the last technique just returns the value of r from myFunction() and assigns it to the variable valueOfR that was made outside of myFunction(). You could then verify values with valueOfR instead of directly accessing r. There are probably a whole lot more solutions to your problem but there are 2 of them
31st Jan 2017, 10:07 AM
Bill
Bill - avatar
+ 13
you have to declare the variable outside the function to use it. variables declared inside a function can only be accessed inside that function. think of each function as a box and the lid can only be opened from inside: the function can grab what's outside or put something out, but you can't reach in its box. you'd have to ask for it politely first.
31st Jan 2017, 9:52 AM
Mario L.
Mario L. - avatar
+ 10
You can also benefit from the pseudo-class model of JS: var Rnd = function() { this.val = null; this.gen = function() { this.val = Math.random(); } } randomValue = new Rnd(); var i = 5; while ( i-- ) { document.write('Old random value = '+randomValue.val+'<br>'); randomValue.gen(); document.write('New random value = '+randomValue.val+'<br>'); } Obviously, if needed, you can create many instances of 'Rnd' class ^^
31st Jan 2017, 11:31 AM
visph
visph - avatar
+ 10
It is called Variable Scope. Please wiki that, there are a lot of examples, don't want to repeat someone's code (DRY rule - don't repeat your self. One of the powerful rule of codding).
22nd Feb 2017, 4:31 PM
Ghena Ramascan
Ghena Ramascan - avatar
+ 7
@visph thanks man your a genius now I am understanding much better where I was wrong I will try to finish and debug it . Thanks again :-D
1st Feb 2017, 3:46 AM
Utkαrsh
Utkαrsh - avatar
+ 7
Make it global. It's within local scope.
4th Feb 2017, 2:16 PM
Mark Foxx
Mark Foxx - avatar
+ 6
thanks I will try my best to fix my program
31st Jan 2017, 10:24 AM
Utkαrsh
Utkαrsh - avatar
+ 6
Check here: https://code.sololearn.com/WeZ9enCY4jTi/#js I'll debug it and comment my modification to make it working... not perfect, but working as is ;)
31st Jan 2017, 8:15 PM
visph
visph - avatar
+ 6
This is because of variable scope. A variable declared in global scope can be accessed at any time we need but a variable declared inside a function can only be accessed within this function. A function creates a new scope. so you can't use them outside of function.
2nd Feb 2017, 12:33 PM
Vikash Pal
Vikash Pal - avatar
+ 5
Actually I'm trying to make a snake game without using any pixel based things by using mainly div for graphics and the most basic JavaScript I want the random position for the snakes food and the verification for confirming that if the foods and snakes position is same then the food gets to a new random position
31st Jan 2017, 6:42 PM
Utkαrsh
Utkαrsh - avatar
+ 5
@visph My code is public now ,I tried many things setInterval() ,etc but nothing seemed to work
31st Jan 2017, 7:02 PM
Utkαrsh
Utkαrsh - avatar
+ 5
@visph if you don't mind where are you from and what's your age
1st Feb 2017, 3:52 AM
Utkαrsh
Utkαrsh - avatar
+ 5
I'm from nowhere, and I'm without age... I don't even know if I really exist: maybe I'm just a bot' :P
1st Feb 2017, 5:58 AM
visph
visph - avatar
+ 4
If you want further advices, post a link to your code ( if you don't want to make it public, else we could find it in your profile ), else you'll get maybe other answers, but in more and more different directions... In example, you can achieve your goal by at least two ways: store in a bidimentional array the map of your game, setting the cell with one code for empty, and one code for a thing at a specific place, or handle that with a list storing positions of things ( food )... And many way to implement it, oo or functionnal...
31st Jan 2017, 6:56 PM
visph
visph - avatar
+ 4
Cause a var inside a function is local. Out of the function, that var doesn't exists.
1st Feb 2017, 11:46 AM
axel vincent
axel vincent - avatar
+ 4
you can do it by assigning it extern , syntax is : extern variable_name;
6th Feb 2017, 7:06 AM
Ankit Dubey
Ankit Dubey - avatar
+ 4
You can use a variable only inside the scope you defined it . Once you define a variable inside a function its scope is defined within the function and hence you can't use it outside the function.
7th Feb 2017, 4:27 PM
J C Karthikeyan
J C Karthikeyan - avatar
+ 3
you have to use return command , like : function myFunction() { var r = Math.random(); return r; } like my friend visph said ❤
31st Jan 2017, 3:02 PM
Abolfazl Vaysi
Abolfazl Vaysi - avatar
+ 3
the best reason is the scope of that variable... that is the variable declared in that block belongs to that part of the block only .. if so however u want to use the variable any where in the programme u could use global variable.. in that way u can use that variable anywhere in the programme
1st Feb 2017, 3:41 PM
Shivam Dubey
Shivam Dubey - avatar
+ 3
The problem is that you're creating a variable that is scoped to the block it is contained in, then trying to access it outside of that scope. Bill has posted a good solution which is to declare a global variable that will store the result of calling the function that provides your random number, which needs to return the value to be of use. An example: var foodLocation = foodGen(); /* this variable can be used anywhere */ function foodGen() { let r = Math.random(); /* this variable is only usable within the confines of this function. To be "used" elsewhere, it needs to be returned, as below */ return r; }
3rd Feb 2017, 7:16 AM
Jordan Klocko
Jordan Klocko - avatar