0

I have an if-else condition in js as below and I am expecting else condition to execute for the below logic, because the destination ID is in else if, but it always goes into the if condition. tried with == and === too.

What am I missing? Note: The logic is to be supported in ie11

// destinationId is generated dynamically but i have assigned it to a variable for reference 
Code 1 
var fromId = "createNotification";
var destinationId ="detailedDescription";

if ((fromId == "createNotification") && (destinationId == "carNumber" || "setNumber")) {
  //logic to execute
} else if ((fromId == "createNotification") && (destinationId == "faultRepBy" || "detailedDescription")) {
  //logic to execute
}
code 2
if ((["createNotification"].indexOf(fromId) > -1) && (["carNumber" || "setNumber"].indexOf(destinationId) > -1)) {
//logic to execute

} else if ((["createNotification"].indexOf(fromId) > -1) && (["faultRepBy" || "detailedDescription"].indexOf(destinationId) > -1)) {
//logic to execute

}
fcelo
  • 9
  • 1
  • 3
    Duplicate of [Check variable equality against a list of values](/q/4728144/4642212). Please try using the [debugging capabilities](//ali-dev.medium.com/how-to-easily-debug-in-javascript-5bac70f94f1a) of your browser. `destinationId == "carNumber" || "setNumber"` is _never_ `false`. – Sebastian Simon Sep 11 '21 at 06:42
  • What should i change it to ? I tried to debug but could nt find hints – fcelo Sep 11 '21 at 06:49
  • Use `(["carNumber", "setNumber"].includes(destinationId))` for 1st one. – TechySharnav Sep 11 '21 at 06:49
  • The answers tell you: `[ "carNumber", "setNumber" ].includes(destinationId)`. – Sebastian Simon Sep 11 '21 at 06:50
  • Either use [`Array.prototype.includes`](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/includes) - as mentioned above or just write `destinationId === "carNumber" || destinationId === "setNumber"` – F. Müller Sep 11 '21 at 06:54
  • var fromId = "createNotification"; var destinationId ="detailedDescription"; if (fromId == 'createNotification' && (destinationId == 'carNumber' || destinationId =='setNumber')) { //logic to execute } else if ((fromId == "createNotification") && (destinationId == "faultRepBy" || destinationId =="detailedDescription")) { //logic to execute } – Rishab Vaigankar Sep 11 '21 at 06:54
  • @SebastianSimon include is not supported in ie11 – fcelo Sep 11 '21 at 07:03
  • @fcelo Then use a [polyfill](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#see_also), or the `indexOf` alternative; it’s all in the answers of the linked question. IE itself is not supported on a lot of sites. – Sebastian Simon Sep 11 '21 at 07:06
  • @SebastianSimon i changed the code with indexof , please see code 2 in question, but it not working – fcelo Sep 11 '21 at 07:07
  • @fcelo You’re misusing `indexOf`. `"faultRepBy" || "detailedDescription"` is `"faultRepBy"`, which is something you could’ve found out by using your debugging tools. Please, again, refer to the [linked question’s answers](/a/4728164/4642212). `indexOf` should be used on an array containing the strings you want to compare against. `[ "faultRepBy" || "detailedDescription" ]` is _not_ an array that contains the strings you want to compare against; `[ "faultRepBy", "detailedDescription" ]` is. `([ "createNotification" ].indexOf(fromId) > -1)` can be simplified to `fromId === "createNotification"`. – Sebastian Simon Sep 11 '21 at 07:08

1 Answers1

0

Change your condition as below:

if ((fromId == "createNotification") && (destinationId == "carNumber" || destinationId == "setNumber")) {
 console.log("in if")
} else if ((fromId == "createNotification") && (destinationId == "faultRepBy" || destinationId == "detailedDescription")) {
 console.log("in else if")
}
shri_world
  • 36
  • 5