Diego López
Diego López

Reputation: 1609

It is a good practice multiply by a boolean on javascript?

Javascript allow using a boolean on arithmetic expressions but I want to know if is a good practice to do so.

For example:

var expectedGuests = 100 * ((day==='Friday')*1.5 || 1);

I know this can be written in different ways but I want to know if using this means a negative or maybe positive impact on performance, navigator compatibility or any other important matter that I should be aware of.

Upvotes: 2

Views: 1156

Answers (1)

tymeJV
tymeJV

Reputation: 104775

Use a ternary, it'll be cleaner:

var multiplier = day === 'Friday' ? 1.5 : 1;
var expectedGuests = 100 * multiplier;

Upvotes: 7

Related Questions