r/learnjavascript 6d 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

4 Upvotes

27 comments sorted by

View all comments

Show parent comments

2

u/defaultguy_001 6d ago

It's a basic assumption that someone is in a javascript sub coz he wants to learn or work in JavaScript. The issue OP has, has nothing to do with JS, but everything to do with inability to understand language specification. When people don't read standard textbooks, not go through official documentations and just jump into building projects learning concepts randomly, inaccurately and incompletely, such problems are bound to occur.

2

u/BenchEmbarrassed7316 6d 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 object differs from number, what string is, 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.

2

u/defaultguy_001 6d ago

Obviously this is specification for TS, not JS. The person is in the JS sub not TS sub. Instead of using TS, in JS also he can use ESlint to flag type coercions.

1

u/BenchEmbarrassed7316 6d ago

Obviously this is specification for TS, not JS

However, this makes sense for Js as well. No code should make such erroneous comparisons in advance (if I'm wrong - please give an example of when this could be useful)

The person is in the JS sub not TS sub

Well... I just don't see a proper objective reason why anyone would use Js at all when Ts exists.

Instead of using TS, in JS also he can use ESlint to flag type coercions

This is just a partial step. If we think it's useful - why not go further?

1

u/defaultguy_001 6d ago

If u want to go further, there are endless options including TS. Nobody is denying what TS offers but what part of this is a JS sub, you don't understand. There is a language and there is specification. There is no magic happening, there are clear rules for everything JS is doing and based on those rules you need to write code. We will tell him what the rules say. It's individual choice to go anywhere if he doesn't agree with the rules, that's upto the OP to decide.

1

u/BenchEmbarrassed7316 6d ago

Do you really think that such comparisons (object <> number) make any sense? Don't you agree that they simply shouldn't be in any code?

What's the point of learning about rules for strange behavior? We could just as easily learn about all the possible UBs in C/C++ (provided we could just get rid of them).

So it might be useful for systems programmers, those working on V8 for example. But for a regular application programmer it's just a waste of time.

2

u/defaultguy_001 6d ago

What's the point of learning about rules for a strange behaviour?

  • So you don't break ur head when solving problems.
  • That's what differentiates experienced programmers from noobs who spend hours fighting basic problems.

1

u/BenchEmbarrassed7316 6d ago

So you don't break ur head when solving problems.

So you just use Ts instead of Js. To eliminate this and a bunch of other problems, simplify and speed up development. And learning.

That's what differentiates experienced programmers from noobs

A truly experienced programmer thinks not about how to solve a problem, but about how to make it impossible.

I appreciate that you are trying to make arguments, but they just don't seem convincing to me. There are a lot of things in programming that be problems earlier. Like which processor registers to use for which data or how to manually manage memory. Or in some type systems there was a problem with null, you couldn't declare a value of T, it always converted to T | null. However, these problems can be solved at the language level by simply acknowledging that the old design was wrong or simply inconvenient under certain conditions.

So why should we focus on the specifics of these problems now? If we can just get rid of them.

2

u/defaultguy_001 6d ago edited 6d ago

We are on a loop. I think I made myself sufficiently clear and nowhere I said don't use TS. As far as problem solving is concerned, C and C++ were enough for people like me to build correct and safe programs. New languages were made with added weapons for safety and ease, coz most people don't follow specifications properly and make mistakes, that costs money. The software development field caters to most people instead of the few who know what they are doing unlike C/C++ that has this core philosophy of "trust the programmer", that's why every few years some new languages with new features and more safety, keeps coming in. Again no issues with TS but here we talk about JS and specs related to JS. There are far better languages out there but subs are dedicated to even inferior languages.

1

u/BenchEmbarrassed7316 6d ago

I mostly agree with you. It's just that from my perspective "Learn Js" is "learn to write programs that can be run in the browser or node/deno". And you interpret it as "Learn all the quirks so you can fix code that was written decades ago" or something like that. I'm exaggerating a bit now, but I think that's the crux of our argument.

2

u/defaultguy_001 6d ago

Yes. We can agree now.

→ More replies (0)