Pattern matching in JavaScript