Why 5 in [1, 3, 5] returns false?

Why 5 in [1, 3, 5] returns false?
0.0 0

#1

I have the following code, but while I loop thru the array comparing numbers I get a strange effect. At some point I’m checking if 5 in [1, 3, 5] and it returns false! What is this all about? The other number passes correctly, except the number 5!

function sym(args) {
  var acumulator = [];
  debugger;
  function reduceEach(arr1) {
    var result = arr1.reduce(function (acc, curr) {
      if (curr in acc) {
        acc.splice(acc.indexOf(curr),1);
      }
      else {
        acc.push(curr);
      }
      return acc;
    }, acumulator);

      return result;
      }

  for (var i = 0; i < args.length; i++) {
    var vale = reduceEach(arguments[i]);
  }
  return vale;

  }

console.log(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]));

#2

The in operator on arrays is problematic - I don’t use it. For one its name is misleading - also it has unexpected behavior - I prefer a simple for loop or for-of to iterate over values in an array - there’s also includes to find a value

5 in [1, 3, 5]
false
2 in [1, 3, 5]
true

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for…of

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for…in#Array_iteration_and_for…in


#3

Thanks @ppc for the input, did not knew that. Why put something in a language that does not work! Well its js. That’s why I like more Python.


#4

Ahh i see it is not checking for matching numbers here if (curr in acc) even though it looks that way when you debug it … but looking at the curr number based on length of acc so i curr num is 2 and acc is [1,2,3] it gives true as it looks at the length as 0,1,2 if curr num had being 3 here it would give false even though there are 3 elements in array it needs 4 as it goes 0,1,2,3
easier to understand if you look at below or maybe run it in repl … as it stands will give true … remove 6 it will give false … its not comparing to see if 4 is a number in arr even though when you debug it looks like that.

if(4 in arr){
  console.log(true)
} else {
  console.log(false)
}```

#5

The nature of software is incremental and continuous improvement - programmers every day have to compromise between getting it completely right and getting something out the door - every programming language has strengths and weaknesses - this is all part of the learning process


#6

So it is searching if there is the index number, not the element itself. Ok, thanks.


#7

Sure, but then there are also things that are plain bad design and that can’t be incremented on, because people are already relying on them in their live code (many of JavaScript’s idiosyncrasies fall into this category). Though to be fair, at least JavaScript is a million times better designed than something like PHP or VBA…


#8

usually this is known in hindsight - I can only be thankful for open source software that is produced and maintained by voluntary effort