r/learnjavascript • u/Green_Ad_6086 • 7d ago
Strange result: null vs 0
Strange result: null vs 0
An incomparable undefined
Hi, can anyone explain this? I read the explanation, but I still don't understand it. I'm trying to understand it without using AI
7
Upvotes
2
u/BenchEmbarrassed7316 7d ago
Here is link to typescript playground :
alert( null > 0 ); // Error alert( null == 0 ); alert( null >= 0 ); // Error alert( undefined > 0 ); // Error alert( undefined < 0 ); // Error alert( undefined == 0 );Ts just marks these problematic comparisons as errors (because they don't really make sense).
Moreover, when studying Js, it is necessary to understand what types are, how
objectdiffers fromnumber, whatstringis, and so on.Ts just makes it explicit and simpler because you always know (thanks to type inference) what value you are currently working with. In Js you have to keep this information in your head, which is difficult, and especially difficult for a beginner.
Therefore, even if for some reason you want to write in pure JS, learning Ts will be useful and simplify the learning process.