Wednesday, 17 January 2018

Test for value in Javascript Array






In SQL Server, I could say:



WHERE X
IN(1,2)


How would you
rewrite the following in
JavaScript:



if (X==1 || X==2)
{}

itemprop="text">
class="normal">Answer





Use href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf"
rel="noreferrer">indexOf to see if
x is in an
array.



if([1,2].indexOf(x) !==
-1)

No comments:

Post a Comment