3 ms·
let published_at = new Date() if (published_at) console.log("it's true!") if (!published_at) console.log("it's false!") As a FE dev I haven't had workplac
by durnygbur 6y ago
let published_at = new Date()
if (published_at) console.log("it's true!")
if (!published_at) console.log("it's false!")
As a FE dev I haven't had workplace with a codebase allowing above for at least 5 years. No one even asks "shall we us JS ot TS?". Strictly enforced static typing all over. It's not that I like it, just no one asks me.
- woutr_be 6y agoWhat's wrong with this? Checking if a variable is defined/null is not exactly uncommon?
- kaeruct 6y agoIn JS, if (!variable) ... coerces the type to boolean, so anything falsy (0, null, undefined, empty string, etc...) will become true. For example if you have a timestamp of 0, it will be counted as false (but is defined and definitely not null)
- woutr_be 6y agoI understand that. But how would strictly enforced typing solve this? Especially when using TypeScript. An incorrect value being given isn’t going to be caught by the compiler.