Search code examples
javascriptdestructuring

Destructuring parameters (TypeError: null has no properties)


I've a function with a video parameter that can be null, and I'm trying to destructure it with defaults. Am coming across a couple of things that are confusing me (even having tried to read about it)

const VideoDetail = ({
    video = {},
} = {}) => {
    console.log(video);
    return 5; 
};

Surely console.log(video) here can't be null and should default to an empty object? It doesn't seem to be. Which is causing me problems in my actual question: Getting TypeError: null has no properties

const VideoDetail = ({
    video = {}, 
    video: { id: { videoId = 0 } = {} } = {}
} = {}) => {
    console.log(videoId);
    return 5;
};

I think it's because video is null. If it was actually an empty default object this wouldn't be happening, right?

What have I missed?

Thanks


Solution

  • Default parameters are only used if the property is undefined (either not existent or explicitly set). Other falsy (null, false etc.) values are not replaced.

    Read more here: Default parameters - JavaScript | MDN