**hello, can someone help me? I have a question, how can I change the value of a variable that is outside of all the functions, but only in one function do I want to call it to change the value? This is an example of what I want to do
var numero =1;
function cambiar(){
numero= 2
}
function llamar(){
numero= ...; //Aquí *
}
*
this is where I want that when calling the variable again it already has the value of the previous function.
The code as you have it written works correctly, since you declared the variable outside the functions, so it is a Global variable.
Since it is a global
numero
variable :cambiar
wherever and whenever you want its value to changeFor example here, we are going to test the variable
numero
inside the functionllamar()
before it changes value and after:Note that since the variable is global , if you use
numero
in the functionllamar
without callingcambiar
, the variable will have the value1
, because in effect there has been no change in it.It is enough to call the change function in your call function, that way you make reference to the value that has a number in that context, otherwise, since your variable is global, if you do not call the function, its value will be 1.