PDA

View Full Version : Check membership in a set in Javascript?



codingman
April 28th, 2012, 12:22 AM
Say, I have a var foo and I want to check if the value of the variable is a member of a set of numbers 1, 3, 4, 6, 7. How would I do that? Any help would be appreciated :)!

Dimarchi
April 28th, 2012, 04:34 PM
Perhaps these links will prove to be educational:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/indexOf

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf

In short, if the answer is anything else than -1 it is in that set of numbers.