You-Dont-Need-Lodash-Underscore
You-Dont-Need-Lodash-Underscore copied to clipboard
includes support for IE 9+
A very simple workaround is
var array = [1, 2, 3];
array.indexOf(1) !== -1;
// → true
Nice to have this tip!thank you @markogresak
Or even simpler:
var array = [1, 2, 3];
!!~array.indexOf(1);
// → true
!!~array.indexOf(4);
// → false
@cht8687 Nice project! :+1:
@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 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 Thanks for another solution. :+1:
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 That's very good point, because NaN !== NaN
. :+1:
Do we still need to worry about IE in 2017?
@stevemao :joy: Lot's of applications still running in IE, especially Banking or government websites....
It's very easy to add a polyfill in most of the cases.
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...
Some features, such as Object.assign
, have a rule despite having no implementation in IE. I think that this issue can be closed.