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

150
Views
Update all object value using global variable

After Update and show In console cartype Safari but inside car object Fiat Expected Output inside car object Safari

I dont want to use car.type = Safari in update function because i used //cartype inside multiple objects.

var cartype = 'Fiat';

var car = {type:cartype, model:"500", color:"white"};

function update(){
  cartype = 'Safari';
}

function show(){
  console.log(cartype);
  document.getElementById("demo").innerHTML = car.type;
}
<p id="demo"></p>

<button id="upd" onclick="update();">Update</button>
<button id="shw" onclick="show();">show</button>

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

0

It seems that your only hope is to set car.type to Safari.

var cartype = 'Fiat';

var car = {type:cartype, model:"500", color:"white"};

function update(){
   cartype = 'Safari';
   car.type = cartype;
}

function show(){
  console.log(cartype);
  document.getElementById("demo").innerHTML = car.type;
}
<p id="demo"></p>

<button id="upd" onclick="update();">Update</button>
<button id="shw" onclick="show();">show</button>

about 4 years ago · Juan Pablo Isaza Report

0

You can use a getter function to get the global cartype from car.type.

var cartype = 'Fiat';

var car = {get type(){return cartype;}, model:"500", color:"white"};

function update(){
  cartype = 'Safari';
}

function show(){
  console.log(cartype);
  document.getElementById("demo").innerHTML = car.type;
}
<p id="demo"></p>

<button id="upd" onclick="update();">Update</button>
<button id="shw" onclick="show();">show</button>

Learn more about getter function: here

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!