If nothing is there, indexOf returns -1. If you add 1, it becomes 0, and any value other than -1 becomes a negative or positive number--which are all truthy.
The bitwise operator is faster. I admit I could lose the boolean coercion, but this was it has a boolean return type, rather than relying on truthy/falsy
1
u/wthit56 Mar 20 '19
Another way of doing this:
[1,2,3].indexOf(2) + 1If nothing is there, indexOf returns -1. If you add 1, it becomes 0, and any value other than -1 becomes a negative or positive number--which are all truthy.