Is a local variable accessible outside 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

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

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.

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 ^^

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).

@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

Make it global. It's within local scope.

thanks I will try my best to fix my program

Check here: //code.sololearn.com/WeZ9enCY4jTi/#js I'll debug it and comment my modification to make it working... not perfect, but working as is ;)

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.

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

@visph My code is public now ,I tried many things setInterval() ,etc but nothing seemed to work

@visph if you don't mind where are you from and what's your age

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

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...

Cause a var inside a function is local. Out of the function, that var doesn't exists.

you can do it by assigning it extern , syntax is : extern variable_name;

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.

you have to use return command , like : function myFunction() { var r = Math.random(); return r; } like my friend visph said ❤

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

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; }

Can local variables be accessed outside the function?

Example. Local variables have Function Scope: They can only be accessed from within the function. Since local variables are only recognized inside their functions, variables with the same name can be used in different functions.

Where are local variables accessible?

Local variables can be accessed with the help of statements, inside a function in which they are declared. You can access global variables by any statement in the program. It is stored on the stack unless specified.

What type of variables are defined outside of a function?

Global variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program. A global variable can be accessed by any function.

Can we access local variable outside the block?

You can not access the variables inside the function unless it is declared outside it.

Chủ đề