Skip to main content

JavaScript and Python: Check if Item is in Array (or List)

JavaScript

We can use indexOf method to do that.

Using indexOf, we'll expect integer output. Returned value -1 (negative one) is to tell that the item isn't present in the array.

In latest version of ECMA (latest browsers), we can use includes method.

Using includes, we'll expect boolean output (true / false).


Python

Well, the phrase itself, "check if item in list", then we can use in operator to do that.

Using that method, we'll expect boolean output (True / False).

DETECTIVE CHECKING ON TROPHIES
Last modified on

Comments