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

Javascript usage of && operator instead of if condition - Stack Overflow

programmeradmin4浏览0评论

What's the point of having this logical operator like this: r == 0 && (r = i);?

function do()
{
    var r = 0;
    var i = 10;
    r == 0 && (r = i);
}

is this the same as:

 if (r==0)
 {
     r=i;
 }

What's the point of having this logical operator like this: r == 0 && (r = i);?

function do()
{
    var r = 0;
    var i = 10;
    r == 0 && (r = i);
}

is this the same as:

 if (r==0)
 {
     r=i;
 }
Share Improve this question asked Jul 25, 2017 at 7:09 user2818430user2818430 6,02921 gold badges90 silver badges155 bronze badges 10
  • Expressions are faster than statements – Rajesh Commented Jul 25, 2017 at 7:10
  • Can be converted to ternary r == 0 ? r = i : r = 10; – Milan Chheda Commented Jul 25, 2017 at 7:11
  • 6 @Rajesh 1) Really? 2) Faster enough to justify the obscure nature of this piece of code? – deceze Commented Jul 25, 2017 at 7:12
  • 4 @Milan If you're going for a ternary, at least make it r = r == 0 ? i : 10 – deceze Commented Jul 25, 2017 at 7:14
  • 3 "What's the point of having this logical operator..." make a one liner paying the price of readability. Generally speaking it is bad code style to use operators to control execution flow. It makes code a bit shorter but less readable. BTW js minifiers would usually do this for you no need to mangle your code in advance :) – Yury Tarabanko Commented Jul 25, 2017 at 7:26
 |  Show 5 more comments

7 Answers 7

Reset to default 7

What always helps me is translating it to words

  1. var r = 0;
  2. var i = 10;
  3. r == 0 && (r = i);

translates to

  1. set variable r to zero
  2. set variable i to ten
  3. if variable r equals zero AND the return of the following statement "set variable r to value of variable i"
  4. do nothing, but r is now 10.

so in short, let's forget about 1 and 2.

In javascript the execution flow in a boolean comparisan is to stop execution of if statement parameters if any part from the && fails.

An boolean comparisan will execute from left to right.

1 == 1 && 2 == 3 && (r = i)

it will pass 1 == 1 fail on 2 == 3 and never reach the assigment operation.

Basically it's a shorthand for:

if(r == 0) {
   r = i;
}

Simple yes r == 0 && (r = i);is same as

if (r==0)
 {
     r=i;
 }

Just tested the speed of the code and the && is little bit faster (almost negligible).

Coming to the actual question, I found the place of using && instead of if us literally short hand code of later. However I never use the code as it highly kill the readability of code reader.

As docs says

Logical operators are typically used with Boolean (logical) values. When they are, they return a Boolean value.

But what we are seeing here is an assignment to the a variable based on other. Of course the code works but I believe, this is just a mis usage of the convention.

It is the same, in terms of logic and control flow.

It is shortening lines of code (code golf) by (ab)using short-circuit behavior.
The StackExchange page for code golf is https://codegolf.stackexchange.com.

For even shorter code, you could use a logical OR as default operator.

r = r || i;

Or

r || (r = i);

UPDATE 2023

With logical OR assignment ||=

r ||= i;

I've been reading some of the answers here and I've come up with this summary.

Short Summary

  • Condition on r: Assign i to r, in case r is null:

    r = r || i
    

    Or

    r || (r = i)
    
  • Condition on i: Assign i to r, in case i is not null:

    i && (r = i)
    

    Or

    r = i || r
    

More Examples

a || (Do Something)    // Means: if `a` is `null`
!a || (Do Something)   // Means: if `a` is **not** `null`

a && (Do Something)    // Means: if `a` is **not** `null`
!a && (Do Something)   // Means: if `a` is `null`

It is indeed the same, and a technique often used by minifiers to collapse code. In this case, you can even use an ! in order to do the if as you are comparing without typecheck:

!r && (r = i);

Or use an || operator for this assignment:

r = r || i;

If you want to keep your code clear, use an if tho.

Consider you have something to print only when r=0 and i=10. then && will be use full like.

if(r==0 && i==10){
   console.log('its good practice')
}

if we use seperate, like

if(r==0){
   if(i==10){
     console.log('its bad practice')
   }
}

what will you do if you have lots of condition to check.? i suggest you to use first one.

发布评论

评论列表(0)

  1. 暂无评论