Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 is that the else statement never got executed even though the if condition was not met. Only the if statement executes no matter what

I have broken down my route to bare minimum trying to figure out the problem. I simplified it to this:

app.get('/test', function(req,res) {

        var category = 'stupid3';

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

            res.end('yup');

        } else {

            res.end('nope');

        }

});

What am I doing wrong?

Why wont the else statement execute?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
536 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...