Logo

Programming-Idioms

History of Idiom 12 > diff from v66 to v67

Edit summary for version 67 by crdrost:
[JS] Because there is a more expressive way to do it

Version 66

2019-09-26, 14:33:23

Version 67

2019-09-26, 14:43:09

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
list.indexOf(x) !== -1
Code
list.includes(x) // newer
list.indexOf(x) !== -1 // older
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.
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