Skip to content

Latest commit

 

History

History
26 lines (18 loc) · 1.82 KB

File metadata and controls

26 lines (18 loc) · 1.82 KB

forEach and this easy #javascript

by Pawan Kumar @jsartisan

Take the Challenge

Consider the following code:

let group = {
  title: "Our Group",
  students: ["John", "Pete", "Alice"],

  showList() {
    this.students.forEach(function(student) {
      // Error: Cannot read property 'title' of undefined
      alert(this.title + ': ' + student);
    });
  }
};

group.showList();

If you run the above code, you will see an error saying "Error: Cannot read property 'title' of undefined". Why does this error occur?

Can you modify the callback function of forEach so that it works correctly without any errors?


Back Share your Solutions Check out Solutions