Aiden Campbell
Aiden Campbell

Reputation: 335

Is there a syntactical way to shorten a long if conditional?

Is there a syntactical way to shorten a long if conditional like in the example below

if (invoice.get('status') !== 'claimed' && invoice.get('status') !== 'paid' && invoice.get('status') !== 'cancelled' )

to

if (invoice.get('status') !== 'claimed' && 'paid' && 'cancelled' )

or something like

if (invoice.get('status') !== 'claimed', 'paid', 'cancelled' )

Upvotes: 1

Views: 38

Answers (3)

amrender singh
amrender singh

Reputation: 8239

You can shorten it by using an Array of status:

if (!['claimed', 'paid', 'cancelled'].includes(invoice.get('status')))

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386624

You could take an array with the not wanted values and check with Array#includes.

if (!['claimed', 'paid', 'cancelled'].includes(invoice.get('status')) {

Upvotes: 4

curlyBraces
curlyBraces

Reputation: 1105

if(!['claimed', 'paid', 'cancelled'].includes(invoice.get('status'))

Upvotes: 1

Related Questions