Javascript extend object
Assuming that if the first object has a property set to undefined and the rule states no change then your code is almost spot on. 

Some minor changes


The variable key into a const.
Return the extended object.
Vet the arguments.


Thus 

function extend(obj1, obj2) {
    if (obj1 !== null &amp;&amp; typeof obj1 === "object") {
        if (obj2 !== null &amp;&amp; typeof obj2 === "object") {
            for (const key in obj2) { 
                if (!obj1.hasOwnProperty(key)) {
                    obj1[key] = obj2[key];
                }
            }
        }
    }
    return obj1;
}
extend({a: 1, b: undefined}, {b: 1, c: 2}); // returns {a: 1, b: undefined, c: 2}