The IF statement got it wrong?

108 views Asked by At

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?

2

There are 2 answers

1
AudioBubble On

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
Jakub Štellner On

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`)
}