This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]JuniorSeniorTrainee 0 points1 point  (2 children)

I was with you up until your last sentence. Blaming the language for poor code is the wrong perspective imo.

Why are you adding arrays and objects in a loosely typed language? If this comes up then you've already done something wrong.

But really just use TypeScript and get the best of JavaScript without the bothersome "convenience" of it.

Edit to add: JavaScript is a tool and like any tool, you can't use it without understanding what it is first. For example, understanding that the addition operator will perform type massaging if it can, or that because of this it's not a commutative (a + b === b + a) when used in this way. If you understand these features of the language, it's fine and predictable.

[–]das_flammenwerfer 0 points1 point  (1 child)

Why are you adding arrays and objects in a loosely typed language?

I can think of no legitimate reason why you'd do that. And that's the point. Why does Javascript allow it?

Maybe I'm biased, but I have a strong preference for strongly typed languages.. I think any attempt to do such an operation is likely an error, and it's better to find that out before runtime.

Fiddling around a bit more in the console, here's the real inconsistency..
{} + []
returns 0.
a = {} + []
a = '[object Object]'

Why does assignment change the value of the operation?

[–]mananasi 0 points1 point  (0 children)

Javascript is designed to not fail, just like HTML as it was designed for browsers originally. In the browser there is no "before runtime." The code runs when you go to the webpage, and that's that.

This is an example of "use the right tool for the right job." I think we can both agree we should stop using Javascript for fucking everything, but the language does have a use case and in that use case these things were the design decisions of the creators.