My comparisons in JavaScript ifs started to look like this:
if (obj1.name === 'apple' || obj2.name === 'apple' || obj3.name === 'apple') {
//code here
}
To do this more efficiently, JavaScript array method “includes” can be used to prevent repetition as follows:
if (['obj1', obj2', 'obj3'].includes('apple') === true) {
//code here
}
First, array containing obj1, obj2 and obj3 is created. For the array method “includes” is used, which gives output true or false, based on if array contains a given string.