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);
thisIsNewNameis 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);
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);
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);