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

215
Views
Functions as variable in Javascript

Following is my code of memoization which is working fine, output "Long Time" once only -

Code -

function memoAdd80() {
  let cache = {};
  return (n) => {
    if(n in cache) {
      return cache[n];
    }
    console.log('Long Time');
    const answer = n + 80;
    cache[n] = answer;
    return answer; 
  }
}

const memoized80 = memoAdd80();
console.log(memoized80(85)); // Long Time 165
console.log(memoized80(85)); // 165
console.log(memoized80(85)); // 165

Though, when writing like this - memoAdd80()(85) this caching doesn't works.

console.log(memoAdd80()(85)); // Long Time 165
console.log(memoAdd80()(85)); // Long Time 165
console.log(memoAdd80()(85)); // Long Time 165

Let me know how can I directly use memoAdd80 instead of creating a memoized variable first and then passing a value to the function.

about 4 years ago · Juan Pablo Isaza
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!