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

154
Views
Cannot use function to update object

I've created a function change which can be used to change the name of the object person.

There is one argument for the function and this argument would be the new name for the object.

But the new name I pass into this argument becomes ...is not defined, what am I missing here?

Edited thisIsNewName is a string and this is to replace Ali.

const person = {
  name: 'Ali',
  Age: '18'
}

function change(text) {
  person.name = text;
}

change(thisIsNewName);

console.log(person.name);

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

0

thisIsNewName is a string and this is to replace Ali.

No, it's not.

thisIsNewName is not defined in your code.

Either define the variable and assign a string

const person = {
  name: 'Ali',
  Age: '18'
}

function change(text) {
  person.name = text;
}

const thisIsNewName = 'newName';
change(thisIsNewName);

console.log(person.name);

or call the function with a string literal

const person = {
  name: 'Ali',
  Age: '18'
}

function change(text) {
  person.name = text;
}

change('thisIsNewName');
console.log(person.name);

about 4 years ago · Juan Pablo Isaza Report

0

You are trying to pass argument that you did not define. You can create variable or you can pass directly string to it

const person = {
  name: 'Ali',
  Age: '18'
}

function change(text) {
  person.name = text;
}

let thisIsNewName = 'ALI2';
change(thisIsNewName);


//Or you can pass directly string
//change("thisIsNewName");


console.log(person.name);

about 4 years ago · Juan Pablo Isaza Report

0

You should pass the name as a string if you meant it like this -> change("thisIsNewName") But if you want to pass a variable to the function then define it before using: let thisIsNewName = "new name"

const person = {
  name: 'Ali',
  Age: '18'
}

function change(text) {
  person.name = text;
}

change("thisIsNewName"); //*

console.log(person.name);

or

    const person = {
      name: 'Ali',
      Age: '18'
    }

    function change(text) {
      person.name = text;
    }
    
    let thisIsNewName = "new name";//*
    change(thisIsNewName);

    console.log(person.name);

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!