0

i need to compare a variable with multiple values

let test = 'e'
if(test == 'q' || test == 'w' || test == 'e') console.log('TEST')

Can you make this entry shorter?

I present it this way, but it is not correct.

let test = 'e'
if(test == 'q' || 'w' || 'e') console.log('TEST')
Roman
  • 175
  • 2
  • 3
  • 15

1 Answers1

1

You can use an array:

const test = 'e'
if (['q', 'w', 'e'].includes(test)) {
  console.log('TEST');
}
sergdenisov
  • 8,327
  • 9
  • 48
  • 63