Replace undefined with zero

Let's assume that we declare an empty object:

javascript
let Erik = {};

console.log(Erik.childs);
// undefined

To cast an undefined variable to zero, we could use the nullish coalescing operator (ES2020):

javascript
// If childs is undefined or null, return zero
Erik.childs = Erik.childs ?? 0; 

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.