Search code examples
javascriptjavascript-objects

How can I add firstName and lastName to fullName?


person = { firstName: 'Joseph', lastName: 'Magnolia', ageInYears: 34 }

function addFullName(personObj) {
  person = { fullName: firstName + lastName, ageInYears: 34, }
}

It tells me undefined, and I have tried different things.


Solution

  • your code doesn't really make any sense, but did you mean something like this:

    var person = { firstName: 'Joseph', lastName: 'Magnolia', ageInYears: 34 };
    addFullName(person);
    console.log(person);
    
    function addFullName(personObj) {
          personObj.fullName = personObj.firstName + ' ' + personObj.lastName;
        }