Logo

Programming-Idioms

History of Idiom 12 > diff from v91 to v92

Edit summary for version 92 by programming-idioms.org:
[JS] Only 1 way per implementation

Version 91

2019-11-01, 21:59:45

Version 92

2019-11-01, 22:02:18

Idiom #12 Check if list contains a value

Check if list contains a value x.
list is an iterable finite container.

Illustration

Idiom #12 Check if list contains a value

Check if list contains a value x.
list is an iterable finite container.

Illustration
Extra Keywords
array vector
Extra Keywords
array vector
Code
return list.includes(x) // newer
return list.indexOf(x) !== -1 // older
Code
return list.indexOf(x) !== -1;
Comments bubble
Array.prototype.includes() is preferred but if you are supporting browsers that are 5+ years old, for example IE 11 , and you are not using a transpiler, then the old syntax is generally well-understood.
Comments bubble
Array.prototype.includes() is preferred but if you are supporting browsers that are 5+ years old, for example IE11, and you are not using a transpiler, then the old syntax with indexOf is generally well-understood.
Doc URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
Doc URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf