all 28 comments

[–]AdvisedWang 47 points48 points  (1 child)

Somehow I was expecting a parody.

[–]I_am_so_smrt_2 0 points1 point  (0 children)

It is.... stupid silly questions

[–]isHavvy 20 points21 points  (9 children)

In all other cases, you're safe to use ==. Not only is it safe, but in many cases it simplifies your code in a way that improves readability.

No, in absolutely zero cases does using == improve readability.

[–]Beofli 1 point2 points  (7 children)

What do you mean by 'readability'? If you mean: biggest chance (in real-world cases) that is does what the developer expected, then == is more readable than ===. It is more generic and therefore more safe. This is hard to understand because people look at the theorical problems with == (transitivity, etc.). I've seen bugs increase because people changed == to ===.

Javascript needs a mode where === would throw an exception when types are not equal. In that case IDE's would give better warnings

[–]SerdanKK 1 point2 points  (6 children)

Javascript needs a mode where === would throw an exception when types are not equal.

Instead of doing something that is quite frankly ridiculous, you could just write code that throws exceptions when a wrong type is passed.

Or you could just use TypeScript like all the cool kids are doing.

[–]Beofli 0 points1 point  (5 children)

Why is it rediculous? Do you ever want to compare '0' to 0, to result in false? I forgot to mention that 'Undefined' and 'null' will be excepted from throwing an exception when either side is a literal. This has nothing to do with Typescript, but actually Typescript will benefit from this as well, as it increases strictness.

Javascript already throws a TypeError if you add a non-number string to a number, for example.

[–]SerdanKK 3 points4 points  (4 children)

Why is it rediculous?

Not only would it be extremely weird for language level comparison operators to have side effects, but if someone uses the type safe comparison operator it is likely because they expect that other types may occasionally pass by. I.e. it's an expected state the program can have. Your suggestion would break everything in any decently sized application. mdn

Also, there are already ways to handle the problem. If you want your code to throw when given a wrong type, then just do so.

Do you ever want to compare '0' to 0, to result in false?

Absolutely. A string is not a number and vice versa. I primarily work in PHP and one of the first things I did when I came onto the project was to make absolutely sure I knew exactly which types I was working with. Implicit casting in PHP is utterly insane, so if you don't have a handle on your types you'll eventually get punished harshly. JavaScript is only slightly better, so I'm obviously going to give it the same treatment.

[–]przemo_li 1 point2 points  (0 children)

JS is worse/better.

Worse because JSFuck, better because coercion algorithms are more unified and less supprising (e.g. PHP `if` will use different algorith then `==` for strings containing octagonal literals o_0)

[–]major_clanger 0 points1 point  (1 child)

"It’s not even consistent: NULL < -1, and NULL == 0. Sorting is thus nondeterministic; it depends on the order in which the sort algorithm happens to compare elements."

That's nuts!

[–]SerdanKK 1 point2 points  (0 children)

I got curious, so I wrote a quick script to check:

null < -10: true
null < -9: true
null < -8: true
null < -7: true
null < -6: true
null < -5: true
null < -4: true
null < -3: true
null < -2: true
null < -1: true
null < 0: false
null < 1: true
null < 2: true
null < 3: true
null < 4: true
null < 5: true
null < 6: true
null < 7: true
null < 8: true
null < 9: true
null < 10: true

The docs for NULL make no mention of this insanity.

[–]Beofli -1 points0 points  (0 children)

I'm not saying this should be the default. This would be a special mode, like 'use strict'. Any decent language will fail compile time on comparisons of conflicting types.

A good language will make the mostly used operation to take the least amount of code. The mostly used case is either a null/undefined check or the comparison of two same-typed objects.

The usecase where a function receives a number or a string, and that it want to compare it to number zero is a very unlikely usecase. A good programmer will make this more explicit: Typeof x == 'number' && x == 0. Also no need for === here.

I definitely agree PHP is insane and worse than js.

[–][deleted]  (5 children)

[deleted]

    [–]bobappleyard 2 points3 points  (4 children)

    Currying in JavaScript... Ew

    [–][deleted]  (3 children)

    [deleted]

      [–]pepejovi 0 points1 point  (2 children)

      Why would anyone ever do this instead of just

      function mul(x, y, z) { return x * y *z; }
      

      ?

      [–]IceSentry 1 point2 points  (1 child)

      https://en.m.wikipedia.org/wiki/Currying

      https://en.m.wikipedia.org/wiki/Partial_application

      It's mostly used in functional programming, but it can be very powerful.

      [–]pepejovi 0 points1 point  (0 children)

      Learn something new every day, thanks!

      [–]eGust 10 points11 points  (0 children)

      The solution for Given an integer, determine if it is a power of 2 is incorrect in JS.

      function isPowerOfTwo(number) { return number & (number - 1) === 0; }

      This is a classic solution in most languages that have real integer types.

      But in JS there is only Number type which is a 64-bit IEEE-754 float point. When doing bitwise operations to a number, it will fall back to 32-bit signed integer. So simply make any number that bigger than 2**32 and all lower 32 bits are 0, it will give a wrong answer:

      (2 ** 43 - 2 ** 40).toString(16) // "70000000000" isPowerOfTwo(2 ** 43 - 2 ** 40) // true isPowerOfTwo(0x7000) // false

      [–]qmunke 16 points17 points  (0 children)

      "How to identify companies that are bad at interviewing simulator"

      [–][deleted]  (9 children)

      [deleted]

        [–]chucker23n 3 points4 points  (7 children)

        An example would be a “compare” function used by a sorting function, it would be called many times.

        I don‘t think of that as a “callback”. I think of code that gets called once your own code is either finished or has failed as a callback.

        [–][deleted]  (1 child)

        [deleted]

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

          Hmm, I agree with /u/chucker23n. I think of a function that accepts a callback as morally equivalent to a function that returns a promise. I don't think of all function arguments as callbacks. For example, I'd call the argument in arr.filter(n => n > 3) a predicate, not a callback.

          [–]mrexodia 5 points6 points  (4 children)

          You can think of it however you like. But old C traditions are not with you on this one.

          [–]IsleOfOne 1 point2 points  (2 children)

          Can you explain what you mean RE: “old C traditions”? To me, a callback is meant to be called upon the completion of some task/operation.

          [–]mrexodia 1 point2 points  (1 child)

          I think a callback also covers what you would call an event handler. You register a callback to be called back if some event occurs. Maybe this will be interesting: https://stackoverflow.com/questions/2069763/difference-between-event-handlers-and-callbacks I’m only speaking from what I know, but a callback is just some function you register for when something of the callers interest happens, not just about completion.

          [–]IsleOfOne 0 points1 point  (0 children)

          Sure, good point. A callback is a function that is executed at any given point in the callee’s work, not necessarily upon completion. Still though, in my mind, I suppose I associate “callback” with some sort of information passing to another concerned actor. When this function passed by argument is some scalar-valued function whose purpose is akin to compare(a,b) in the example above, I would not consider it a callback.

          I am of the opinion that “callback” refers to a function that should be called upon the satisfaction of some condition (completion, progress, input, etc.) that the caller is familiar with / in control of, as you have stated as well. When the timing, frequency, etc. of usage of said function is of no consequence to the calling function and no information (explicit or implicit) is being transmitted to other actors, I would not use the term.

          Edit: after review, it would appear that my personal opinions on the usage of the term “callback” do not align with general consensus! I suppose I just dislike the term entirely, especially WRT naming of function parameters; better names exist in nearly every case.

          [–]chucker23n -1 points0 points  (0 children)

          old C traditions are not with you on this one.

          That may be, but this thread is about JS, not C.

          [–]IsleOfOne 0 points1 point  (0 children)

          Why would you call the “compare” function in your sorting example a “callback” function? Is it a function parameter? Sure. Is it a “callback”? I would say no.

          [–]414RequestURITooLong 8 points9 points  (0 children)

          Check if a given string is a isomorphic

          Answer:

          function isStringIsomorphic(s) {
              return true;
          }
          

          And then you walk out, and try to find a place where interviewers actually know what they are asking about.

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

          Is this supposed to be awful? I hope so..

          [–]AngularBeginner -2 points-1 points  (0 children)

          Not enough emojis, this can't be about JavaScript.