Do JavaScript arrays have an equivalent of Python’s “if a in list”?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

This basically tells about true or false. If the item is found then it returns true, else false is returned. You can achieve the same in JavaScript arrays, using includes.

Example

Following is the code −

var nameList =
   [
      "John",
      "David",
      "Bob",
      "Carol",
      "Sam"
   ] 
console.log(nameList.includes("Bob"));
console.log(nameList.includes("Adam"));

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo286.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo286.js
true
false
raja
Published on 09-Nov-2020 13:00:14
Advertisements