Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

231
Vistas
How to access a variable value that is defined inside another function JS

I have a variable totalActivatedOffers which is defined inside a function, inside an IF statement. I need to be able to use the value of this variable outside of the IF statement by another function but can't get it to work, it keeps returning undefined.

I've declared it at the top of the script block like this:

var totalActivatedOffers;

But still no joy. Here is an example of how the value is assigned to it (simplified for illustration):

if ("SSO" in sessionStorage) {
  setTimeout(function(){
    if (firstArrayLength.length == 3) {
      var totalActivatedSparksOffers = 4;
    }
    if (secondArrayLength.length == 3) {
      var totalActivatedRewardsOffers = 7;
    }
    var totalActivatedOffers = totalActivatedSparksOffers + totalActivatedRewardsOffers;
  }, 2500) 
} // closes if SSO is inside session storage

// need to access the value here, outside of the IF statement
console.log('Total activated offers to subtract is ' + totalActivatedOffers);

Any help would be greatly appreciated.

SOLUTION:

Managed to get around the issue by making it a window variable like this:

window.totalActivatedOffers = totalActivatedSparksOffers + totalActivatedRewardsOffers;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Managed to solve the issue by making it a window variable:

window.totalActivatedOffers = totalActivatedSparksOffers + totalActivatedRewardsOffers;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda