What is “x && foo()”?

前端 未结 5 1917
遇见更好的自我
遇见更好的自我 2020-11-22 13:34

I saw somewhere else said,

x && foo();

 is equal to

if(x){
    foo();
}

I tested it and they really di

5条回答
  •  余生分开走
    2020-11-22 14:01

    In javascript, the && operator evaluates left to right and returns the value of the rightmost operation. If the first condition evaluates to false, it doesn't evaluate the second. So its a shorthand of saying "if something is not null or undefined, do something"

提交回复
热议问题