DEV Community

Discussion on: I'm Javascript. And I'm done taking your shit.

 
jpantunes profile image
JP Antunes

Please... can you be a bit more clear?

> let a = new Boolean
undefined
> a
[Boolean: false]

First, a Boolean value is one of { true, false }. If it's undefined then it's not a boolean, but it might work as one thanks to the falsy / truthy inference. So, if you pass an undefined value into a boolean check it will evaluate as falsy which is the sane thing to do, but if you are in fact working with Booleans (the type) then you will initialise your variable appropriately. Here is a bit more info in case you want / need it: github.com/lydiahallie/javascript-...

Again, if someone writes poor code in any language, the code will be poor. If you try to write Java in JS you'll have a bad experience but that's really about knowing the tools you work with, right?

Thread Thread
 
190245 profile image
Dave

You get that the OP posted with humour, and as numerous other commentors have said, the same can be said of any language, right?

Why the need to defend JS? There's aspects of the language I don't like (and can't really avoid). There's aspects of Java I don't like (and can actively avoid. See: Optional). There's times when JS is the appropriate tool to use, so I use it. There's other times when other languages are more appropriate, and they get used.

As I originally said in reply to the OP (I think, it was a while ago), my main issue with JS is that some people see JS as a panacea. No language is a panacea, they all have down sides.

Thread Thread
 
jpantunes profile image
JP Antunes

Yeah it does seem like I'm defending JS but my intention is only to understand the actual reasons behind the complaints. IMHO it's often due to lack of knowledge, and I say that from my own experience since I used to dislike JS at a fundamental level.. until I actually decided to learn it as a new language instead of trying to "hack" Java into it.

Thread Thread
 
190245 profile image
Dave

My only complaint with JS, isn't even to do with JS.

As I originally posted in the comment on this, my complaint is the people that see JS as a panacea.

Appropriate tool, at the appropriate time, and I'm happy. Hell, my boss this morning was kicking around an idea to use scala + MLlib, my reply was "can't say I've used either enough to have any sort of opinion... give me a few days & I'll knock together a spike so we can at least explore."

Thread Thread
 
simme profile image
Simme

I'm still very curious about what you meant with undefined Booleans, though. 😅

Thread Thread
 
190245 profile image
Dave • Edited

Again, note the capitalisation.

Consider the following JS:

var someBool;
if (someBool) { console.log("true"); } else if (!someBool) { console.log("false") }
someBool = true;
if (someBool) { console.log("true"); } else if (!someBool) { console.log("false") }
someBool = false;
if (someBool) { console.log("true"); } else if (!someBool) { console.log("false") }

Now, consider that coming from a strictly typed background, the above code shows a boolean variable (not a Boolean) in 3 different states, when the very intent of booleans is a binary state.

If one is not aware of that particular nuance, it leads to frustration & wasted time. Of course, you could make the argument that if you don't understand the nuances of a language, why are you writing in it... but where's the fine line in "sufficient knowledge" there? And why would a Project Manager looking at a deadline care if you know enough or not?

I love the fact that the two people to want to discuss this point, read "boolean" as "Boolean" :D

Thread Thread
 
jpantunes profile image
JP Antunes • Edited

The only data type where the variable name and its value are correlated are symbols (or atoms, or literals depending on what the language you use calls them).

In the example you give, var someBool is no more a boolean than var cow is a bovine animal in any dynamic or statically typed language I can think of at the moment.

What the if statements are evaluating is whether or not the value type is undefined which is only very thinly related to booleans.

For reference:
developer.mozilla.org/en-US/docs/G...
developer.mozilla.org/en-US/docs/W...

Thread Thread
 
simme profile image
Simme • Edited

😅

Well, booleans can only be true or false. however, a variable named in a way that makes it seem like it should reference a boolean can, of course, be uninitialized if you allow uninitialized declarations. And I guess that's the thing; javascript by default allows you to declare uninitialized vars.

So; yes - javascript is useless at being java, c#, or any other strongly typed language, in the same way, those languages are useless at being javascript. But that's not really a fair comparison as they have traits not applicable to the other.

Thread Thread
 
190245 profile image
Dave

Nor did I say it was a fair comparison, nor did I even really say it's an issue (of any size).

But surely, you can see that - as was the intent of my reply to the other person, from an OO/strictly typed background, JavaScript is indeed very easy to get wrong. That's no different to any other language if we only have surface knowledge in them.

In other languages, such as Java, the primitive boolean datatype exists, and null exists, but a boolean can never be null, it is either true or false. Again, that's because of strict typing, and strictly typed languages simply make more sense in my head.

Does that stop me using JS? No... but you were curious, so I expanded on my comment.

Thread Thread
 
190245 profile image
Dave

I'm aware of how to use JavaScript, and do so more times than I'd like, so no need for the documents, or the defence of JS, but thanks.

Thread Thread
 
jpantunes profile image
JP Antunes • Edited

There's no way to respond to that statement productively.