Or operator not working in IF statement Node.js

前端 未结 2 1159
抹茶落季
抹茶落季 2021-01-25 02:02

I originally had a route in node.js that stated this:

If the req.url === something or something or something or something, do this, else, do that.

The problem i

相关标签:
2条回答
  • 2021-01-25 02:42

    the else statement is never executed because your if condition always returns true..

    if (category === 'stupid' || 'stupid2') {
    

    the second part of the condition i.e. after the || operator is 'stupid2 which is a truthy value

    0 讨论(0)
  • 2021-01-25 02:53

    You're using it incorrectly.

    category === 'stupid' || category === 'stupid2'
    

    Your version is effectively...

     (category === 'stupid') || 'stupid2'
    

    ...so because a non-empty string is "truthy", the RHS will always cause the || to pass.

    0 讨论(0)
提交回复
热议问题