15

I almost got caught by this during an interview:

const foo = ['a', 'b'];

const bar = foo.findIndex(x => x === 'a'); // 0

if (bar) { // I'm an idiot
console.log('Do something');
}

🤦‍♂️

Comments
  • 5
    Don’t worry too much. It’s a trap that many of us walk into.
    That’s the reason why some of the good languages got rid of implicit conversions to bool.
  • 1
    Can you add some explanation for people who don't understand this language?
  • 2
    @happygimp0 in JavaScript, 0 evaluates to false. I was going to use that number to retrieve a value from the array, which would have been a bug because it's a valid index number but falsy value.
  • 3
    @happygimp0

    bar is set to 0 due to the findIndex() returning 0 as the index.

    If(bar)

    is checking 0 against true.
    In Javascript 0 is false.
  • 1
    @C0D4 but why would you check against a 0 ?

    You know you are working with javascript so you should know that 0 = false but 0 is a valid index.

    Javascript is know for this type of behavior.

    Never assume implicit conversion is doing what you think it is doing.
  • 8
    Also in C/C++, 0 is both a valid array index and a boolean false. Completely normal.
  • 2
    what does it return when it's not present? isn't -1 the universally accepted standard for fallback index?
  • 1
    @Fast-Nop Maybe not that normal since I can only think of JS and C/C++.
    Are there any other examples? Php maybe (not sure).
  • 0
    @theabbie traditionally, -1, yes. But the modern way would be to return null or something similar.
  • 1
    @Lensflare Not sure either. Even in C, I compare to 0, '\0', or NULL to clarify which kind of 0-ish comparison is going on: value range check, string terminator, or pointer check.

    The only exception are variables intended for purely boolean usage anyway.
  • 0
    @Grumm you are missing the point, they forgor to check if it's not -1.

    Also hecc u i will rely on implict conversions even more
  • 1
    There is no excuse for not doing an explicit identity comparison when your variable isn't actually a boolean.

    Don't shoot yourself, always use explicit comparisons and conversions.
  • 0
    @Fast-Nop NULL=='\0' && NULL==0
  • 0
    @saintograph Yes, false is a fancy way of saying 0, no question there.

    I don't understand this part: foo.findIndex(x => x === 'a')

    And why do you check if bar is not 0?
  • 0
    @C0D4 I don't see the problem. And i still don't understand this part: foo.findIndex(x => x === 'a')

    I mean, yes he gets some kind of index and does something with it when the index is not 0, but i don't understand why. What is special about index 0?
  • 0
    @Fast-Nop There is no false in C.
  • 0
    @happygimp0 I reckon I wrote that due to force of habit. Writing JavaScript means having to constantly do type checking, something which switching to Typescript mitigates to a certain degree.
  • 0
    @saintograph Yes. I am sorry, but i still don't understand what your mistake was. You say it has something to do with types, but i only see 2 types: The array of chars/strings (don't know enough about JavaScript to know if there is a difference) and a integer which can be 0 (of false, if you want to call it that) or non-zero.
  • 1
    @happygimp0 I do know that '\0', NULL and 0 are the same in terms of code, but conveying which kind of 0-comparison is going on makes the code easier to understand for the next dev or even myself after a few months. It's about maintainability.

    Of course there is "false" in C, it's in <stdbool.h>.
  • 0
    I would respond with “what idiot calls their variables ‘foo’ and ‘bar’ - he should not be allowed to even look at the repo”.
  • 0
    @saintograph Ok, now i understand. You didn't want to check for the index but something else, correct?

    Sorry, but your rant is a bit unclear in this regard. When i see that code i think the purpose is to check for index==0 vs something else. I think it is really bad decision to set invalid index to false. False and 0 is exactly the same in many (most?) languages.
  • 1
    When an index can't be found, it's assumed -1, he skipped a mind-step and left it as just the result.
  • 0
    You could do something like this is :

    foo.includes => returns a valid bool true or false

    Then if true, findIndex.

    But again, learning that any array has always a zero-based index is a must.

    bar > -1 will be quicker
Add Comment