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

139
Views
Why can I access an updated array in my function but not another type of variable in Javascript?

Let's say I have a factory function createFunction() with an integer variable and an array object.

function createFunction() {
  let a = 5;
  let arr = [];
  function updateA (value) {
    a = value;
  }
  function getA () {
    return a;
  }
  function updateArr(value){
    arr.push(value);
  }
  return  {a, updateA, getA, arr, updateArr};
}

After updating a and arr, I try to access them.

const myFunction = createFunction();

myFunction.updateA(11);
console.log(myFunction.a); // prints 5
console.log(myFunction.getA()); // prints 11

myFunction.updateArr(33);
console.log(myFunction.arr); //prints [33]

I'm confused why the updated array can be accessed directly with myFunction.arr whereas the variable a cannot be accessed with myFunction.a. They are both exposed via the return in createFunction() and so I assume they have the same scope. Why can the updated value of a only be accessed using a getter method?

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!