Two different results in calling a function

61 Views Asked by At

The class in Udemy I'm going through had me create a function for ordering at a restaurant (to illustrate an example of Destructuring)

I noticed that I got two different results logged when I made a mistake of adding an extra set of brackets around returning a method inside of a function. I tried to explain to myself what the difference was that resulted in the difference but couldn't. It seems like it should be a basic explanation but I can't figure it out.

const restaurant = {
  name: 'Classico Italiano',
  location: 'Via Angelo Tavanti 23, Firenze, Italy',
  categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
  starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
  mainMenu: ['Pizza', 'Pasta', 'Risotto'],

  order: function(starterIndex, mainIndex) {
    return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
  }
}

console.log(restaurant.order(2, 0)); // ["Garlic Bread","Pizza"]

VS

const restaurant = {
  name: 'Classico Italiano',
  location: 'Via Angelo Tavanti 23, Firenze, Italy',
  categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
  starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
  mainMenu: ['Pizza', 'Pasta', 'Risotto'],

  order: function(starterIndex, mainIndex) {
    return [this.starterMenu[starterIndex]], [this.mainMenu[mainIndex]];
  }
}

console.log(restaurant.order(2, 0)); // ["Pizza"]

0

There are 0 best solutions below