Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

357
Visualizações
how to acces variables from another function?

How to acces variable from another function? alert from function 1 takes the sum of the 2 variables from function 2

fun1();

function fun1() {
  alert("var two + var three = " + two + three);
}

function fun2() {
  var one = 1;
  var two = 2;
  var three = 3;
  var four = 4;
}

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Either define these variables globally outside of both functions (a very bad practice for any real world program - but suitable enough for an example) - or provide them as arguments to the alert function.

var one, two, three, four;

fun1();

function fun1 ()
{
  fun2();
  alert("var two + var three = " + two +  three);
}

function fun2 ()
{
  one = 1;
  two = 2;
  three = 3;
  four = 4;
}

fun2();

function fun1 (one, two, three, four)
{
  alert("var two + var three = " + two +  three);
}

function fun2 ()
{
  fun1(1, 2, 3, 4);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

This is not possible to access a variable from outside of Scope

if you want to access it from more functions then make it global. Like below.

fun2();   
fun1();
var two = 2;
var three = 3;
function fun1() {
  alert("var two + var three = " + two + three);
}

function fun2() {
  var one = 1;
  two = two + 2;
  three = three +3;
  var four = 4;
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda