Logo

Programming-Idioms

History of Idiom 12 > diff from v74 to v75

Edit summary for version 75 by fibre:
[JS] Doesn't return a value, like other examples.

Version 74

2019-09-26, 15:39:05

Version 75

2019-09-26, 15:50:43

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