I am super glad that you asked! I forgot the talk about this, and you made a good point. To shorten it, I believe your argument can be translated to, why not using equality instead of identity operation?
Because of clarity. I have to repeat myself: this stuff is not really important if you are working on a smaller project, or in a small team. Based on my experience it’s quite easy to mess up things when we do magic…
Like what? Like this:
function save(post) {
if (post.user) {
post.user = loadUser(post.user);
}
Posts.save(post, function (result) { ... });
}That was the original code, when there was a bug report, that user names are not displayed in a certain case. It turned out, that there was a change in the backend, and post.user was zero in some cases, meaning we didn’t load the user.
What did we learn? First thing, we should communicate better. But mainly that javascript magic has a price we have to pay. In this case the condition was that user is falsy. That could mean a lot of things. And while you are perfectly right, we could solve my original issue with equality operator, I force my coworkers not to do that, because of clear understanding of the code is the most important thing.
And I wanted to focus about how to map an empty or wrapped value without a single condition.