0
I wonder if it is possible for two Javascript functions to share information that is in one of them.
Example:
function a(){
var nome = prompt("digite seu nome")
}
function b(){
alert(nome)
}
0
I wonder if it is possible for two Javascript functions to share information that is in one of them.
Example:
function a(){
var nome = prompt("digite seu nome")
}
function b(){
alert(nome)
}
3
Responding literally to what you said:
I wonder if it is possible for two Javascript functions to share information that is in one of them.
Yes, it is possible. There are basically two mechanisms for this: scope and value pass/return.
Each function creates a new scope, and everything that is declared (with var
, let
, const
) in a scope is not visible outside it:
// aqui é o escopo global
var a = 'escopo global';
function f() {
// f enxerga o escopo global
console.log('f enxerga ' + a);
// f também tem seu próprio escopo
var b = 'escopo local de f';
console.log('f enxerga ' + b);
}
// O escopo global não enxerga o escopo local de f
console.log(b); // ReferenceError
Since one function can be declared within the other, this allows creating one nested chain of scopes. You can explore this mechanism and build your code so you have access where you need it.
A function, by definition, is something that takes one value and returns another - although in JS functions can also function as subroutines, not needing to receive or return anything.
When a function works on a certain value, this value can be passed to it at the time of the call. For example:
function ola(nome) {
console.log('Olá, ' + nome + '!');
}
ola('Douglas');
It can also return a value:
function ola(nome) {
return 'Olá, ' + nome + '!';
}
console.log(ola('Douglas'));
Regarding the question code, you can’t say anything because it doesn’t show your real intention. The examples here serve to guide how things work, and it’s up to you to decide which approach to use in each case.
Both solutions seem simple, and are actually simple. The hard part is learning to handle these basic features to make sure the code is clear, readable and modular. This you only learn in practice, and much practice.
3
Declare your variable outside the function, making it a global variable that can be accessed and modified from anywhere in the code.
var nome = "";
function a(){
nome = prompt("digite seu nome") }
function b(){
alert(nome)
}
1
You can call the function b
which will return what was typed at the function prompt a
. This return will be the value of the variable nome
within the scope of the function a
:
function a(){
var nome = b(prompt("digite seu nome"));
console.log(nome);
}
function b(nome){
alert(nome);
return nome;
}
a();
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.
I didn’t understand why to define the variable
nome
with the return ofb
whereasb
is only a warning. It was very confusing the code, but I think you got confused and changed things by mistake. The intention would not be to return froma
the value read andb
give warning about the return ofa
? At least it seems to make more sense to me.– Woss
Thanks for the explanation, and it didn’t make much sense why I’m applying this concept in a role-playing game, but as it’s getting big, n would put here. And also, the first function is in the context of reading the player’s name, so I wanted the second function to have access to that name.
– Douglas Nunes
@Andersoncarloswoss It is because (at least it seems to me) the variable
nome
will still be used in the function scopea
.– Sam
Yeah, by putting the
return
changes everything :D– Woss
@Sam, wouldn’t a global variable be better? From what I understood the OP would like to share the value between multiple functions, I assume that the question case is just one example. What do you think about at least including this alternative in your answer?
– Paz
@Peace It could be too, although it is always better to avoid global variables. But as the other answer already dealt with it, I will leave mine as it is. Obg!
– Sam