-1

Hey I'm a beginner at javascript, and I've come across this issue. It seems like the IF statement recognizes the variable as something it isn't. I think it has something to do with the OR operator?

let variabel = `a variable with random value`;

if (variabel === `a` || `b`){
    console.log(`the variable is the same as a or b`)
} else {
    console.log(`not the same`)
}

So it does say the variable is the same as a or b in the console. So the if statement is true? But it isn't?

  • 1
    Your if statement is testing whether `variable === 'a'` OR _"b" is true_. Because "b" is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy), the condition evaluates to true. – ray Oct 07 '20 at 21:48
  • Your expression is evaluated as ```(variabel === `a`) || (`b`)```. – Felix Kling Oct 07 '20 at 21:48
  • 1
    Keeping in mind that the template literals aren't getting you anything in this use case. – Taplar Oct 07 '20 at 21:49

2 Answers2

2

The correct syntax is:

let variabel = `a variable with random value`;

if (variabel === `a` || variabel === `b`){
    console.log(`the variable is the same as a or b`)
} else {
    console.log(`not the same`)
}
0

Correct use of OR is this way

let variabel = `a variable with random value`;

if (variabel === `a` || variabel === `b`){
    console.log(`the variable is the same as a or b`)
} else {
    console.log(`not the same`)
}