Problem
function safeUpdate(obj, key, value) {
  if (!obj.hasOwnProperty(key)) {
    obj.key = value;
  }
}

const user = {
  name: "Alice",
  age: 30
};

safeUpdate(user, "country", "USA");
Solution

This one was for the beginners.

function safeUpdate(obj, key, value) {
  if (!obj.hasOwnProperty(key)) {
    obj.key = value;
  }
}

Our bug is that we’re adding a literal key property to our object.

console.log(user.key) // "USA"

In JavaScript, if you want to use a variable as the key of an object, you need to use bracket notation instead of dot notation.

function safeUpdate(obj, key, value) {
  if (!obj.hasOwnProperty(key)) {
    obj[key] = value;
  }
}