最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript how to use the andor operator with parenthesis - Stack Overflow

programmeradmin0浏览0评论

I have an object that contains a string HelloWorld in the attribute hello. I want to check for two strings, and if it does not match with either one, then I want to execute certain code.

var item = { hello: "HelloWorld" }
item.hello !== "HelloWorld" && item.hello !== "GoodbyeWorld" // false, correct

However, I feel this can be optimized and made more readable:

item.hello !== ("GoodbyeWorld" && "HelloWorld") // false, correct
item.hello !== ("HelloWorld" && "GoodbyeWorld") // true   WTF?

I expected both checks to be falsy, but surely I'm missing something here. I think I do not have a correct understanding of the AND/OR operator in javascript, or I am using the parenthesis in a wrong manner. Can anyone explain?

JSFiddle example

I have an object that contains a string HelloWorld in the attribute hello. I want to check for two strings, and if it does not match with either one, then I want to execute certain code.

var item = { hello: "HelloWorld" }
item.hello !== "HelloWorld" && item.hello !== "GoodbyeWorld" // false, correct

However, I feel this can be optimized and made more readable:

item.hello !== ("GoodbyeWorld" && "HelloWorld") // false, correct
item.hello !== ("HelloWorld" && "GoodbyeWorld") // true   WTF?

I expected both checks to be falsy, but surely I'm missing something here. I think I do not have a correct understanding of the AND/OR operator in javascript, or I am using the parenthesis in a wrong manner. Can anyone explain?

JSFiddle example

Share Improve this question asked Sep 30, 2013 at 7:53 Justus RomijnJustus Romijn 16.1k5 gold badges52 silver badges63 bronze badges 1
  • 3 You can't do it like that. I can't think of a single language that supports such a construct. For a more detailed explanation, see stackoverflow./a/17200368/146205. – Jensen Commented Sep 30, 2013 at 7:57
Add a ment  | 

4 Answers 4

Reset to default 3

The result of "HelloWorld" && "GoodbyeWorld" is "GoodbyeWorld" which is why you're getting the result you are, the previous way you were doing it is the simplest solution

Let's take a look at this line

item.hello !== ("HelloWorld" && "GoodbyeWorld") // true WTF?

The logical AND operator evaluates its right operand, if lVal is a truthy value.

Note, a truthy value is every value which is not falsy (null,false,0,"",undefined,NaN)

Since "HelloWorld" is indeed truthy

The expression ("HelloWorld" && "GoodbyeWorld") evaluates to "GoodbyeWorld" and you're effectively paring

item.hello !== "GoodbyeWorld" which can be reduced to "HelloWorld" !== "GoodbyWorld"

Hence, is true


However, if you're on an ES5 patible environment you could use Array.prototype.indexOf to simplify it.

!~["HelloWorld","GoodbyWorld"].indexOf(item.hello) //false

the above returns true if item.hello is not contained in the array

No, you can't optimise the expression that way. What you are doing is elliminating one of the strings, so that you are only doing one of the parisons.

The && operator uses short circuit evaluation, which means that if the first operand is truthy, it will just return the second operand.

So, what your code is doing is to pare the hello property value to the second one of the strings, which explains the results that you get.

item.hello !== "HelloWorld" && item.hello !== "GoodbyeWorld"

is the proper way to test whether item.hello is distinct from "HelloWorld" and "GoodbyeWorld".

An expression A && B in JavaScript yields a result which is either A or B and it's that result, that is pared to your item.hello.

发布评论

评论列表(0)

  1. 暂无评论