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

160
Views
Create a function that updates a variable in any scope

Is there any way to reassign a variable passed into a function?

(Note: I am not asking if there is any way to reassign a variable, whether in global or parent scope. My goal is to create a function that can set ANY variable in ANY scope to a value.

I understand pass by reference and pass by value - and I'm wondering if there is a way to have functions change the memory reference of a variable in any scope (not just a parent scope).

Example:

const setAnyVarTo5 = function(inputVar) {
  // set the inputVar to 5, regardless of its type.
  inputVar = 5; // this is only setting the local variable inputVar. Even if we passed in a reference to an object, we'd still just be changing inputVar's reference, not x's.
}

let x = 7;
setAnyVarTo5(x);
console.log(x); // 7, but desired output is 5

let y = {};
setAnyVarTo5(y);
console.log(y); // {}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A direct answer to the question is "no". It's also not really a great idea to mutate variables like that.

If you can add more context of what you're trying to achieve, I'd be glad to update the answer with a solution.

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!