Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda