Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
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 answers
Answer question

0

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

window.totalActivatedOffers = totalActivatedSparksOffers + totalActivatedRewardsOffers;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!