DEV Community

Discussion on: You don't need null

Collapse
 
matyasjay profile image
Matyas Angyal

Sure, I'm considering it not-a-bug for the same reason, it is just the part of the language and you have to deal with it :) However, you can exploit its potential. I personally prefer using null over undefined when I want to consider an asset to be explicitly empty (but still defined in the scope of the current code). undefined is the default value for a non-existent value and I think it is unsafe to rely on this, because it can cause runtime errors more often (think of global window in Node environment). It's all about practices and personal preference. In my opinion, null is a handy stuff and I'm happy to use it.

 
loucyx profile image
Lou Cyx • Edited

It's all about practices and personal preference

100% agree with this! My series of "you don't need" is not about imposing my style, is more about making the readers wonder: Do I really need that? Or I'm just used to it? I used to be a C++ hardcore fan, class all the way, and my first entry in this series is saying that we don't need them because I feel happier not using them in JS/TS.

About undefined introducing bugs, the vast majority of them are solved with the good ?? and ?., so you can actually do stuff like:

const obj = {};

obj?.level1?.level2; // undefined, no errors
obj?.method?.(); // undefined, no errors

obj?.level1?.level2 ?? "default value"; // "default value", no errors
obj?.method?.() ?? 10; // 10, no errors
Enter fullscreen mode Exit fullscreen mode
 
matyasjay profile image
Matyas Angyal

Yup, exactly. Hail to nullish coalescing and chaining! I love how people eager to build the language forward and constantly improving it in every year. Classes are not quite making sense in JS to me too. Although, it is important to be able to adjust your personal preference to the environment your working in. Everyone is doing stuff differently, but we usually work in teams, so... you know the drill.

 
loucyx profile image
Lou Cyx

Indeed! My No.1 priority at work daily is making my teammates lives easier, building components, utils and libraries for them. More than once I had to change something I personally like to something that adjust to them better, and that's ok. They are all happy without having to deal with classes, but if they weren't, I would be doing classes even if I don't use them in my personal projects, just to adjust to them ❤️

Some comments have been hidden by the post's author - find out more