You-Dont-Need-Lodash-Underscore icon indicating copy to clipboard operation
You-Dont-Need-Lodash-Underscore copied to clipboard

includes support for IE 9+

Open markogresak opened this issue 8 years ago • 12 comments

A very simple workaround is

var array = [1, 2, 3];
array.indexOf(1) !== -1;
// → true

markogresak avatar Jan 27 '16 08:01 markogresak

Nice to have this tip!thank you @markogresak

cht8687 avatar Jan 27 '16 09:01 cht8687

Or even simpler:

var array = [1, 2, 3];
!!~array.indexOf(1);
// → true

!!~array.indexOf(4);
// → false

@cht8687 Nice project! :+1:

IonicaBizau avatar Jan 27 '16 14:01 IonicaBizau

@IonicaBizau I do not know why would that be simpler, other than in number of characters used. The readability of this code is bad and might even be confusing for people who do not know about unary operators and how bitwise negation works, which is especially true for new programmers. Performance wise, there is next to none difference.

markogresak avatar Jan 27 '16 19:01 markogresak

@markogresak Well, simpler === shorter for me. :joy: No worries, I know what you mean. Especially in an if condition, you just use if (~array.indexOf(42)) { /* yes, it exists */ }.

IonicaBizau avatar Jan 27 '16 20:01 IonicaBizau

@IonicaBizau Thanks for another solution. :+1:

cht8687 avatar Jan 27 '16 21:01 cht8687

You do need to take care of the case of NaN, because [NaN].indexOf(NaN) === -1 even though [NaN].includes(NaN) === true:

function arrayIncludes(arr, search/*, fromIndex*/) {
  'use strict';
  if (null == arr) throw new Error('The first argument to arrayIncludes may not be null or undefined.');
  var a = Object(arr), len = parseInt(a.length, 10) || 0, n, k, curr;
  if (0 === len) return false;
  n = parseInt(arguments[2], 10) || 0;
  if (n >= 0) k = n;
  else k = len + n;
  if (k < 0) k = 0;
  for (; k < len; k++) {
    curr = A[k];
    if (search === curr || (search !== search && curr !== curr)) return true;
  }
  return false;
}

The last if statement in this example is where I test for the special case of NaN; this is adapted from a polyfill I wrote once for Array#includes, and the example in this repo doesn't need to be as detailed as this one.

lewisje avatar Feb 02 '16 15:02 lewisje

@lewisje That's very good point, because NaN !== NaN. :+1:

IonicaBizau avatar Feb 02 '16 17:02 IonicaBizau

Do we still need to worry about IE in 2017?

stevemao avatar May 17 '17 22:05 stevemao

@stevemao :joy: Lot's of applications still running in IE, especially Banking or government websites....

cht8687 avatar May 18 '17 00:05 cht8687

It's very easy to add a polyfill in most of the cases.

stevemao avatar May 18 '17 02:05 stevemao

IE 9-11 is really heavily used, more so in more conservative industries (banking, military contractors, etc), so yeah... IE11 is the last version for Windows prior to 10, no Edge support...

tracker1 avatar May 30 '17 23:05 tracker1

Some features, such as Object.assign, have a rule despite having no implementation in IE. I think that this issue can be closed.

stevenvachon avatar May 07 '18 01:05 stevenvachon