Be careful with null values in JavaScript

Be careful with null values in JavaScript

Null values in JS are funny. It’s easy to confuse them with undefined. This happens especially in React components where you want to deconstruct props and get a default value.

const { user, friends={}, review={} } = props;
console.log(friends);
console.log(review);

Consider that friends prop is undefined and review prop is null, friends will be logged as an empty object as expected but review will be logged as null and not an empty object, that’s because null is a value in JavaScript, actually to be more precise the type of null is an object. Thank you JavaScript for your magical nuances.