Anurag Mishra
Anurag Mishra

Reputation: 699

How to reduce if else condition?

I want to reduce an if else condition in JavaScript. How can I do it?

The existing code is - <Select isDisabled={data === 'test'? true : false} /> It is working fine.

But I want to check only true condition so I am doing like - <Select isDisabled={(data === 'test' && true)} />

It is not working fine. So how can I print only true conditon. I don't want to check false for isDisabled condition in Select Tab

Upvotes: 0

Views: 73

Answers (2)

wangdev87
wangdev87

Reputation: 8751

=== operator returns the boolean value, so you can just use data === 'test'

<Select isDisabled={data === 'test'} />

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074138

You just use data === 'test', which already gives you a boolean:

<Select isDisabled={data === 'test'} />

There's never any reason for relationalExpression ? true : false, because the result of a relational expression (like ==, ===, <, >, etc.) is already a boolean (true/false).

Similarly, relationalExpression ? false : true is generally best written by changing the operator (=== to !==, <= to >, etc.), reversing the operands, or (for operators like in where neither of those is possible), negating the result: !(relationalExpression).

Upvotes: 2

Related Questions