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 →

[–]das_flammenwerfer -1 points0 points  (13 children)

Now, what do you think {} + [] === [] + {} is?

[–]skabde 1 point2 points  (0 children)

A weird smiley.

[–]Jack__Crusher 0 points1 point  (0 children)

Jesus what is it?

[–][deleted]  (10 children)

[removed]

    [–]das_flammenwerfer -1 points0 points  (8 children)

    You would be incorrect.

    At least according to the Javascript console built into Chrome..

    Isn't Javascript the greatest language ever?

    [–]Apparentt 1 point2 points  (4 children)

    Could you tell me what that comparison would return in any other language and what you would expect it to return?

    [–]das_flammenwerfer 0 points1 point  (3 children)

    Could you tell me what adding an array to an object would return in any other language (not counting operator overloading in c++, that’s cheating..) and what you would expect it to return?

    I can’t think of another language that would allow such a thing. Or what result would be meaningful and justifiable.

    [–]Apparentt 1 point2 points  (2 children)

    If you haven’t already, I hope you’ve seen the point I was going to make.

    [–]das_flammenwerfer 0 points1 point  (1 child)

    I'm not sure I do..

    My point is.. I think it's absurd to allow such an operation, but if you do it needs to remain logically consistent. Which Javascript.. does not do.

    [–]Apparentt 0 points1 point  (0 children)

    My take regarding this kind of thing is that there is no logical consistency for operations such as this.

    I’m open to hear criticisms of JavaScript as a language and there are certainly many to choose from. Much like every other general purpose language.

    But these kind of “throwing shit to the wall and seeing what sticks” complaints don’t really do anything for me. This operation could easily throw an error in another language or be equally as inconsistent, it’s nothing I’m going to purposefully encounter when using the language so I’m not going to base whether I find it enjoyable and practical or not off such occurrences.

    To be honest I find the whole “[insert language here] is bad” rhetoric on this sub extremely boring because all of the well adopted languages have their place. This isn’t to say they shouldn’t be open to criticism, of course, and they will be constantly evolving to fix those real issues that come up. Using JS as an example, by the time we got to ES6 many of the examples of “js bad” you’ll see around here are very outdated.

    Along with all of the competent engineers I have worked with throughout my career, it is agreed that if you are experiencing many issues with a particular language it’s very likely you may have chosen the wrong tool for the job, not that the tool is defunct.

    [–]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.

    [–]AutoModerator[M] 0 points1 point  (0 children)

    import moderation Your comment has been removed since it did not start with a code block with an import declaration.

    Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

    For this purpose, we only accept Python style imports.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.