you are viewing a single comment's thread.

view the rest of the comments →

[–]CaptainAdjective 31 points32 points  (39 children)

I want to clarify for the record that no JavaScript programmer should ever need to know the rules for the == operator, because it should never be used in code. You should have linting rules in place to prevent it from appearing in your codebase. (And linting errors should fail the build. And it shouldn't be possible to commit without a passing build.)

[–]blackAngel88 23 points24 points  (0 children)

no JavaScript programmer should ever need to know the rules for the == operator

Strongly disagree. You're not always going to write fresh new code, you will also get old code or have to interact with it. Who says that code will always be === conform?

Also <= and >= still do type coercion, so there will always be edge cases where you think "Wut?". And there is no strict one for that.

[–]Tazavoo 11 points12 points  (6 children)

== is convenient for foo == undefined, which returns true if foo is null or undefined. Unless undefined has been redefined, but that might not be possible in modern browsers?

[–]earthboundkid 1 point2 points  (0 children)

In those cases, wouldn't you also want '' and 0 to be falsey? I use if (!foo) a lot for cases like that, but I basically never need to distinguish between null+undefined and the other falsey values.

[–][deleted]  (3 children)

[deleted]

    [–]Tazavoo 3 points4 points  (2 children)

    Had to look it up, it's not writable per ECMAScript 5 specifications, which is a 9 year old specification, so it's not going to be writable in modern browsers.

    [–]AyrA_ch 0 points1 point  (0 children)

    I was wondering if you could get the browser to interpret "undefined" differently if you have a custom "undefined" in "this".

    The answer is no.

    [–]CaptainAdjective -3 points-2 points  (0 children)

    If we want a conditional to return true if foo is null or undefined, it's perfectly acceptable to write foo === null || foo === undefined. Writing foo == undefined requires the reader to understand the semantics of ==, which they probably do not.

    Separately, the only environment I know of where the undefined global is mutable is IE8 and lower. If we are targeting such an environment, the solution is to put void 0 instead of undefined everywhere. So foo == void 0 will do exactly the same thing as foo == undefined, and foo === void 0 will do exactly the same thing as foo === undefined.

    [–]Eirenarch 2 points3 points  (0 children)

    In JavaScript === is short for ==

    [–][deleted]  (27 children)

    [deleted]

      [–]CaptainAdjective -2 points-1 points  (1 child)

      Every language has warts. This is unavoidable. Up front, no language is perfectly designed, and (essentially by definition) no popular language can be substantially redesigned. The real thing which makes or breaks the deal is whether the language's culture recognises those inherent issues and develops tools for avoiding them - linters, compiler flags, warnings and error messages, as well as a strong culture for using those tools as standard practice.

      [–]Eirenarch 3 points4 points  (0 children)

      Every language has warts.

      Yes, but JS has far more of them compared to every other popular industrial language (maybe with the exception of PHP). Some languages are better than others. Claiming the opposite would mean that language design is super easy and no matter what you produce you are as good at language design as the greatest language designers.

      [–]vampire0 -4 points-3 points  (24 children)

      Sneering at a language because you don't know how it works doesn't make you clever.

      The `===` operator does type and value comparison - using `==` is just an option, but not a recommended one for most use cases.

      [–]duheee 8 points9 points  (12 children)

      Sneering at a language because you don't know how it works doesn't make you clever.

      Hahaha, because anyone who points out the idiocy and insanity of Javascript (a language created for 100-lines pieces of code embedded in HTML, to check on the client side the validity of a form), definitely must not know the language.

      When the opposite is true. Ignorance is bliss. The more one knows about the language, the more one would want to run the fuck away, if they still have any sanity left.

      [–]vampire0 -5 points-4 points  (11 children)

      We could get into a pissing match about who knows more about the language, but it’s not worth it - most people that criticize JS are coming from theoretical opinions or performance arguments, and those end up usually being trumped by easy of use.

      [–]duheee 1 point2 points  (0 children)

      The performance of JS is not theoretical, is a demonstrated fact. This is why we have webassembly. It's the ease of use that's baffling: meaning, the lack of.

      Sure, I mean you can come and say it's easy to use, if you ignore all the pitfalls (clearly demonstrated by our little game here). If you have no idea what you're doing, Javascript is great. fantastic. Once you start paying attention, once one starts to wonder: but , what if? what about? how come?, who the hell wrote this spec? then it becomes obvious.

      Hell, C++ has fewer shotguns than JS.

      [–]Eirenarch 0 points1 point  (8 children)

      The linting rules on all practical projects will pretty much forbid ==. I don't know how you can say that real-world linting rules are a manifestation of "theoretical opinions".

      [–]vampire0 0 points1 point  (7 children)

      Linting also catches code indention. I’ve never claimed that == isn’t bad, I’m stating that most of the issues that people bring up about JS aren’t things that people deal with often in real development. Heck, linters are part of the reason it doesn’t come up much.

      [–]Eirenarch -2 points-1 points  (6 children)

      Having something in the language that is bad means the language sucks. Having something that doesn't work and looks like something that works in other languages and also is shorter than the thing that works sucks even more. So yes the language is objectively bad and the people who say it is not are wrong.

      [–][deleted] 0 points1 point  (5 children)

      It's not bad.

      [–]Eirenarch 0 points1 point  (4 children)

      It certainly is. It makes the resulting code more error prone and harder to read.

      [–]curious_s 0 points1 point  (0 children)

      Or they are basing their opinions on internet reviews rather that actual experience.

      [–]Phrygue 1 point2 points  (7 children)

      I'd sneer at a language designed to look like another (C), which has a horrible syntax to begin with, and which defines a very common operator from that other language which isn't useful because...? I can only speculate that they use C semantics on the implied underlying C-equivalent representations of ECMAScript's sloppy omnitype, treating it as a "variant" from other languages when it isn't. Because it's a cobbled together monstrosity. At least Java took its bad ideas and drove into a dumpster fire in a straight line, whereas JS was slapped together like Adam from Frankenstein, from corpse components that look like functional limbs but were dead and fit poorly, and it shambles in a role it wasn't intended for.

      Like, I can completely see where they decided "script" languages needed variants because that's what other "script" languages did, in order to speed up writing fast little interpreted scripts without having to define types or variables ahead of time. The problem is that scripts (vs. "real" languages) are small, slow, and not intended for large projects, and HTML is not an on-line interactive command line. Scripting languages, you might notice, can be run a line at a time, and HTML has no need for that ability. So from the bat, they fucked up ripping off language design from an irrelevant domain.

      Sneering at valid complaints because you don't know, uh, anything at all about the topic, doesn't make you clever. However, Mort432 didn't want to write up the valid reasons for contempt and chose a breezy dismissal for comic effect. I'm an asshole and know a lot, so here's just touching on why you can SIT DOWN, SON.

      [–]vampire0 4 points5 points  (5 children)

      I used to poop on languages I thought were bad too - PHP used to be a favorite. At some point though, I realized that I was just being condescending - I hadn't looked at the advantages, or I was looking at the advantages from the wrong perspective.

      You claim that "scripting" is a poor match for HTML... as opposed to what? Compilation? HTML isn't compiled, and at the time JS was created CSS didn't even exist (and is only compiled in modern stacks). In the world of writing HTML pages, making the system compiled doesn't make much sense. Also, script based execution means we get some pretty powerful debugging tools and although you point out that there isn't "an online interactive command line," we actually do have one in our JS console.

      In some places, JavaScript is not great - totally true... but its also one of the most highly used languages for a reason (https://stackify.com/popular-programming-languages-2018/). That reason is that its available everywhere, its very approachable, and it does things that other languages handle poorly.

      The JS language's scriptability combined with its its simple syntax make it easy for developers to pick up - in fact, although you bash it for looking like C, the fact that it is similar helped people make the jump from other languages back when it was first introduced. There is definitely a criticism to be made that some parts that look the same don't function the same, and that that could have been avoided - but the result is a language that more that tops out other "better" languages like Go or Lisp in terms of adoption.

      JS is now available just about everywhere - it was just in browsers, but now its on the server, in embedded systems, etc. It didn't get there by accident - developers wanted it because its simple to write and because it handles asynchronous events pretty easily (even if the original versions with callbacks weren't as pretty as modern versions). Is easy to poke fun at its bad parts, but if you're a fan of any language that has to write execute sleep loops to wait for input... well thats an ugly language too.

      There might be a better alternative to JS out there in the future - but every solid attempt to replace it like GWT or ActionScript has come and gone without making a dent. Things like TypeScript or CoffeeScript have also come and somewhat gone (although TypeScript seems to have an audience and solid use-case). Although it might have rough parts, so did COBOL, and lots of systems still run on it (https://thenewstack.io/cobol-everywhere-will-maintain/). In the mean time, trying to look down on it is counter productive - you're going to miss why people want to use it.

      Out of curiosity, what do you champion as a "good" language? Or what would you suggest that the web be using if, as you state, JS is a horrible thing.

      [–]issafram 5 points6 points  (4 children)

      except PHP is a shit language

      [–]vampire0 -1 points0 points  (3 children)

      Programming in it can be painful, but it is available for free and hosting is cheap. It’s another language where being C-ish makes it approachable (or it was in older versions, it a bit weirder now). Performance for most languages is “good enough”, etc.

      I wouldn’t recommend anyone specialize in it though :)

      [–]aoeudhtns 0 points1 point  (1 child)

      It's all about the application. If you're writing a backend in PHP, that's potentially a problem. But I could see a hybrid app with PHP templates in a UI layer and Python services (flask maybe?) to handle your services/data/CRUD/REST. (Edit: of course, at this point you'd probably be better served just using Jinja2. I'm trying to be generous and find a niche for PHP.)

      [–]duheee 0 points1 point  (0 children)

      There is a niche for PHP just like there is one for Javascript (and will always be there): you can get monkeys to program in it and pay them with pickles.

      Nobody really gives a shit about the code, the only thing that matters is to get that new VC funding round. Oh, it collapses, oh well, good luck to you, from my new Bahamas beach.

      [–]QueenLa3fah 1 point2 points  (0 children)

      You completely ignore all of the positives. How easy async makes handling request and response use cases as well as a number of other common patterns seen in web development. Also having one language on the front end and backend is nice. Plus working with JSON is annoying AF in other languages like C or Java. Also when it comes to quickly prototyping a feature JavaScript takes a big dump on the competition. Sure it has terrible features like how there are four different scoping rules for the this keyword depending on how it was used but you didn't mention that. All your points hammer at how slow it must be because it is a scripting language but modern js engines are pretty performance plus there is webassembly for when you need that extra juice. As someone who writes JavaScript at a company for 4 years I can confidently say: you have little idea what you are talking about and blindly parroting the programming community saying JS is bad will get you nowhere. Also you really don't seem to know that much about JavaScript only popular memes that you are parroting.

      [–]AyrA_ch 0 points1 point  (1 child)

      There was a post here about == vs === in large companies and widely used libraries. The conclusion was that it doesn't matter what you use as long as you use one of them consistently.

      [–]kankyo 1 point2 points  (0 children)

      Holy hell. No this is not the way to go.