admin管理员组

文章数量:1341390

I'm Getting confused

I tried to have to have a two condition, in the first code I'm getting true answer seems the answer should be false.

var a = 'route 3';
if(a === 'route 1' || 'route 2'){
  console.log('true')
}else{
  console.log('false')
}

on the second code, I have two conditions and the first condition it seems true and the second must be false but i'm getting true answer. base on my understanding if the first condition is false the second condition will not be evaluated. can you enlighten my newbie mind why i'm getting true answered.

var a = 'route 1';
var b = 'route 3';

if(a === 'route 1' || 'route 2' && b === 'route 1' || 'route 2'){
  console.log('true')
}else{
  console.log('false')
}

thank you for the answer :)

I'm Getting confused

I tried to have to have a two condition, in the first code I'm getting true answer seems the answer should be false.

var a = 'route 3';
if(a === 'route 1' || 'route 2'){
  console.log('true')
}else{
  console.log('false')
}

on the second code, I have two conditions and the first condition it seems true and the second must be false but i'm getting true answer. base on my understanding if the first condition is false the second condition will not be evaluated. can you enlighten my newbie mind why i'm getting true answered.

var a = 'route 1';
var b = 'route 3';

if(a === 'route 1' || 'route 2' && b === 'route 1' || 'route 2'){
  console.log('true')
}else{
  console.log('false')
}

thank you for the answer :)

Share asked Feb 16, 2019 at 1:27 Jomar TingaJomar Tinga 671 gold badge2 silver badges12 bronze badges 1
  • You need to have a === 'route 2' in the second part of the condition. otherwise you are just checking if 'route 2' is a truthy value, which it is since a non empty string is always true in JavaScript. – George Commented Feb 16, 2019 at 1:28
Add a ment  | 

4 Answers 4

Reset to default 3

Changing your first code block to this will fix your problem:

var a = 'route 3';
if(a === 'route 1' || a === 'route 2'){
  console.log('true')
}else{
  console.log('false')
}

The || operator does not work the way you think. What you are saying is: is a equal to route 1? || is route 2... (instead of: is a equals to route 2). The same issue applies to your second example:

var a = 'route 1';
var b = 'route 3';

if(a === 'route 1' || a === 'route 2' && b === 'route 1' || b === 'route 2'){
  console.log('true')
}else{
  console.log('false')
}

More on operators: Javascript operators

In this statement

if(a === 'route 1' || 'route 2' && b === 'route 1' || 'route 2')

the part that sais || 'route 2') will always evaluate to true.

it must be if(a === 'route 1' || (a === 'route 2' && b === 'route 1') || a === 'route 2')

when you evaluate if('route 2') that would check for a value which always evaluates to true. You would have to pare it to something.

You are forgetting to check if a is equal to 'route 2', when you simply do if('route 2'). After using ||, you still need to explicitly define the next condition, which is a === 'route 2'.

Instead use:

var a = 'route 3';
if(a === 'route 1' || a === 'route 2'){
  console.log('true')
}else{
  console.log('false')
}

Or even more concise:

var a = 'route 3';
console.log(a === 'route 1' || a === 'route 2');

Problem:

'route2' returns true because there is no paring ie. a === 'route2' the string will always return true

Solution:

var a = 'route 3';
if(a === 'route 1' || a === 'route 2'){
  console.log('true')
}else{
  console.log('false')
}

本文标签: javascriptif condition on node jsStack Overflow