Getting a clear expression with a special conditional statement

I will show you a small part of my application where I am wondering what is the right way to set the condition I'm working on. If in both cases I show you that you are right, I would like you to tell me the consequences / hardships

if ((some.thing === '' || 0) || (some.how === '' || 0)) { //something is going on here } 

that's how I have it so far, is there anything bad with it?

or should be better:

 if ((some.thing === '' || some.thing === 0) || (some.how === '' || some.how === 0)) { //something is going on here } 

So what are your suggestions? is this the same result at the end?

EDIT

Adding another way:

 if (some.thing === '' || some.thing === 0 || some.how === '' || some.how === 0) { //something is going on here } 
+6
source share
5 answers

Operator || takes precedence over comparison operators.

So some.thing === '' || 0 some.thing === '' || 0 same as (some.thing === '') || (0) (some.thing === '') || (0) . It:

  • will be evaluated to 0 if some.thing === '' is false or
  • will be evaluated to true if some.thing === '' is true .

Take a look at this example (how it works in the JavaScript console):

 > some = { thing: 0 } Object { thing: 0 } > some.thing === '' false > some.thing === '' || 0 // this is like false || 0 0 > some = { thing: '' } Object { thing: "" } > some.thing === '' true > some.thing === '' || 0 // this is like true || 0 true 

Prefer something like the last expression you wrote.

EDIT: Actually both '' and 0 are falsy in JavaScript, so you can just write your full expression as:

 if (!some.thing || !some.how) { //something is going on here } 
+3
source

This part looks suspicious to me:

 some.thing === '' || 0 

You mean this instead:

 some.thing === '' || some.thing === 0 

0 always false, so the expression some.thing === '' || 0 some.thing === '' || 0 always equivalent to some.thing === ''

EDIT

You need the final expression:

 (some.thing === '' || some.thing === 0 || some.how === '' || some.how === 0) 
+1
source

|| not like the SQL IN clause. Basically this is what you do:

 someBoolean || 0 

so it never compares some.thing with a safe type with Number 0 .

0
source

In the first example, you basically do this:

 if ((condition1 || condition2) || (condition3 || condition4)) 

Now condition 1 and condition 3 can be true or false, depending on the contents of some.thing and some.how, but conditions 2 and 4 will always be false (0 is false), so basically what you say ,

 if (condition1 || condition3) 

Second example:

 if (condition1 || condition2 || condition3 || condition4) 

Your third example looks something like this:

 if ((condition1 || condition2) || (condition3 || condition4)) 

And now they can all be false or true (for both the second and the third example).

Keep in mind that if, for example, some.thing is '' while some.how is 'something', the whole set of conditions will be true.

0
source

Use the second code snippet. The first code snippet is invalid because

 if ((some.thing === '' || 0) || (some.how === '' || 0)) { //something is going on here } 

fist evaluates some.thing === '' , which may be true for false, then it returns this result OR false, so your code finally

 if (some.thing === '' || some.how === '') { //something is going on here } 
0
source

Source: https://habr.com/ru/post/989307/


All Articles