Why do I get "undefined?


#1

I’m learning about “this”…

https://www.codecademy.com/courses/learn-javascript-objects/lessons/objects/exercises/this?action=lesson_resume&course_redirect=introduction-to-javascript

Can anyone help me why the text string is coming back as “Hello undefined, how are you?”

let person = {
  name: 'Tyron',
  age: 40,
  weekendAlarm: "No alarms needed",
  weekAlarm: "Alarm set to 7am",
  sayHello: () => {
    return `Hello ${this.name}, how are you?`;
  },
  sayGoodbye() {
    return 'Goodbye!';
  }
};

console.log(person.sayHello());

#2

You need to use es6 for your sayHello method to work correctly with this:

sayHello: function() {
    /* code */
  },

which can be shortened to:

sayHello() {
    /* code */
  },

so far my understanding goes, the behavior of this was changes for es6


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.