- Courses
- JavaScript
- Objects
- Object.assign
- Handling null or undefined in Object.assign
How does Object.assign()
treat null
or undefined
sources?
Object.assign() ignores null or undefined sources.
When Object.assign()
is called with null
or undefined
as one of the source objects, it simply ignores them and does not throw an error. This means that only the properties from valid objects are copied to the target object, and any null
or undefined
sources are skipped.
const target = { a: 1 };
const source = null;
const result = Object.assign(target, source);
console.log(result); // { a: 1 }