0 votes
in JavaScript by
What's the output?
const food = ['', '', '', ''];
const info = { favoriteFood: food[0] };

info.favoriteFood = '';

console.log(food);
  • A: ['', '', '', '']
  • B: ['', '', '', '']
  • C: ['', '', '', '', '']
  • D: ReferenceError

1 Answer

0 votes
by

Answer: A

We set the value of the favoriteFood property on the info object equal to the string with the pizza emoji, ''. A string is a primitive data type. In JavaScript, primitive data types don't interact by reference.

In JavaScript, primitive data types (everything that's not an object) interact by value. In this case, we set the value of the favoriteFood property on the info object equal to the value of the first element in the food array, the string with the pizza emoji in this case (''). A string is a primitive data type, and interact by value

Then, we change the value of the favoriteFood property on the info object. The food array hasn't changed, since the value of favoriteFood was merely a copy of the value of the first element in the array, and doesn't have a reference to the same spot in memory as the element on food[0]. When we log food, it's still the original array, ['', '', '', ''].

Related questions

0 votes
asked Mar 13 in JavaScript by DavidAnderson
0 votes
asked Mar 10 in JavaScript by DavidAnderson
...