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

158
Views
Is there a way to change a variable’s value given its name in a function?

Is what I’m trying to accomplish possible? I hear using globalThis is the best alternative to window, frames, etc. but I can’t get it to function. Any help would be appreciated!

var x = true;
var y = true;

function makeFalse(changeVar) {
  globalThis.changeVar = false;
}

makeFalse("x");
makeFalse("y");

console.log(x); //expecting: false, returning: true
console.log(y); //expecting: false, returning: true
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can update them using global window object or with this or with globalThis inside the function.

this and globalThis inside makeFalse function refers to global window object itself.

var x = true;
var y = true;

function makeFalse(changeVar) {
  // window[changeVar] = false;
  // this[changeVar] = false;
  globalThis[changeVar] = false;
}

makeFalse("x");
makeFalse("y");

console.log(x); //expecting: false, returning: true
console.log(y);

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!