或者运算符不在IF语句Node.js中工作 [英] Or operator not working in IF statement Node.js

查看:123
本文介绍了或者运算符不在IF语句Node.js中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初在node.js中有一条路线说明了这一点:

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

如果req.url ===东西或东西或什么东西,这样做,这样做。

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

问题是即使if条件不满足,else语句也从未执行过。只有if语句执行无论什么

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');

        }

});

我做错了什么?

为什么不会执行else语句?

Why wont the else statement execute?

推荐答案

你是使用不正确。

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

你的版本是有效的......

Your version is effectively...

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

...因为非空字符串是真实的,RHS将总是导致 || 通过。

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

这篇关于或者运算符不在IF语句Node.js中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆