https://stackoverflow.com/questions/2802055/what-does-the-construct-x-x-y-mean

--------------------------------------------------------

What is the double pipe operator (||)?

The double pipe operator (||) is the logical OR operator . In most languages it works the following way:

  • If the first value is false, it checks the second value. If it's true, it returns true and if it's false, it returns false.
  • If the first value is true, it always returns true, no matter what the second value is.

So basically it works like this function:

function or(x, y) {
if (x) {
return true;
} else if (y) {
return true;
} else {
return false;
}
}

If you still don't understand, look at this table:

      | true   false
------+---------------
true | true true
false | true false

In other words, it's only false when both values are false.

How is it different in JavaScript?

JavaScript is a bit different, because it's a loosely typed language. In this case it means that you can use || operator with values that are not booleans. Though it makes no sense, you can use this operator with for example a function and an object:

(function(){}) || {}

What happens there?

If values are not boolean, JavaScript makes implicit conversation to boolean. It means that if the value is falsey (e.g. 0""nullundefined (see also All falsey values in JavaScript)), it will be treated as false; otherwise it's treated as true.

So the above example should give true, because empty function is truthy. Well, it doesn't. It returns the empty function. That's because JavaScript's || operator doesn't work as I wrote at the beginning. It works the following way:

  • If the first value is falsey, it returns the second value.
  • If the first value is truthy, it returns the first value.

Surprised? Actually, it's "compatible" with the traditional || operator. It could be written as following function:

function or(x, y) {
if (x) {
return x;
} else {
return y;
}
}

If you pass a truthy value as x, it returns x, that is, a truthy value. So if you use it later in ifclause:

(function(x, y) {
var eitherXorY = x || y;
if (eitherXorY) {
console.log("Either x or y is truthy.");
} else {
console.log("Neither x nor y is truthy");
}
}(true/*, undefined*/));

you get "Either x or y is truthy.".

If x was falsey, eitherXorY would be y. In this case you would get the "Either x or y is truthy." if y was truthy; otherwise you'd get "Neither x nor y is truthy".

The actual question

Now, when you know how || operator works, you can probably make out by yourself what does x = x || y mean. If x is truthy, x is assigned to x, so actually nothing happens; otherwise y is assigned to x. It is commonly used to define default parameters in functions. However, it is often considered a bad programming practice, because it prevents you from passing a falsey value (which is not necessarily undefined or null) as a parameter. Consider following example:

function badFunction(/* boolean */flagA) {
flagA = flagA || true;
console.log("flagA is set to " + (flagA ? "true" : "false"));
}

It looks valid at the first sight. However, what would happen if you passed false as flagAparameter (since it's boolean, i.e. can be true or false)? It would become true. In this example, there is no way to set flagA to false.

It would be a better idea to explicitly check whether flagA is undefined, like that:

function goodFunction(/* boolean */flagA) {
flagA = typeof flagA !== "undefined" ? flagA : true;
console.log("flagA is set to " + (flagA ? "true" : "false"));
}

Though it's longer, it always works and it's easier to understand.


You can also use the ES6 syntax for default function parameters, but note that it doesn't work in older browsers (like IE). If you want to support these browsers, you should transpile your code with Babel.

See also Logical Operators on MDN.

shorthand trick with boolean expressions的更多相关文章

  1. POJ | Boolean Expressions

    总时间限制: 1000ms  内存限制: 65536kB 描述The objective of the program you are going to produce is to evaluate ...

  2. Boolean Expressions POJ - 2106 (表达式求值)

    The objective of the program you are going to produce is to evaluate boolean expressions as the one ...

  3. [poj 2106] Boolean Expressions 递归

    Description The objective of the program you are going to produce is to evaluate boolean expressions ...

  4. Boolean Expressions

    Boolean Expressions Time Limit: 1000MS   Memory Limit: 30000K       Description The objective of the ...

  5. POJ 2106 Boolean Expressions

    总时间限制: 1000ms  内存限制: 65536kB 描述 The objective of the program you are going to produce is to evaluate ...

  6. (栈的应用5.2.2)POJ 2106 Boolean Expressions(表达式求值)

    /* * POJ_2106.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...

  7. POJ 2106 Boolean Expressions (布尔表达式求值)

    题意:关于!,&,| 的运算,表达式中V代表true,F代表false. 思路:见代码吧,很详细了. 要注意 !!!F,!(...) 的情况. #include <iostream> ...

  8. poj 2106 Boolean Expressions 课本代码

    #include<cstdio> const int maxn=100 +10; int val[maxn],vtop; int op[maxn],otop; void insert(in ...

  9. POJ 2106-Boolean Expressions,双栈运用类似表达式求值!

    Boolean Expressions 首先声明此题后台可能极水(毕竟这种数据不好造!).昨天写了一天却总是找不到bug,讨论区各种数据都过了,甚至怀疑输入有问题,但看到gets也可以过,难道是思路错 ...

随机推荐

  1. virtualbox测试k8s要注意的情况

    想在virtualBox上测试k8s,遇到两个情况要注意.. 第一是flannel和dashborad起不起来,master都无法正常..这时可以想办法把Iptables,selinux,firewa ...

  2. Go语言练习之方法,接口,并发

    多练练,有感觉了就写实际的东东. package main import ( "fmt" "math" "os" "time&qu ...

  3. 主机批量扫描工具fping,hping安装及使用

    https://blog.csdn.net/weixin_39762926/article/details/79476196?utm_source=blogxgwz0 https://blog.csd ...

  4. hdu5756

    http://www.cnblogs.com/duoxiao/p/5777644.html 官方题解在这里 其实这道题不难,当初训练的时候不会做说明自己太弱 lazy标记不pushdown就是用laz ...

  5. ASP.NET MVC5(一)—— URL路由

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  6. vmware + ubuntu 64 安装 node.js v8.9.3

    第一次使用虚拟机,第一次使用linux系统,第一次安装使用node.js 虚拟机安装不用多说,安装好之后下载ubuntu 64位版本文件 在vm中点击“创建新的虚拟机”,选择下载的ubuntu iso ...

  7. Eclipse Qt开发环境的建立

    1.下载Eclipse目前Eclipse+CDT已经可以集成下载了,好像优化过了,速度还比较快.下载的地址是:http://www.eclipse.org/downloads/,选择“Eclipse ...

  8. (6) go 流程控制

    一. if else (1)如果只有一条语句,大括号不能省略 (2)右括号 和 else 在一行 (3)支持 if 时可以定义变量 (4)if 的风格尽量不要加括号,用空格代替 (5)多分支 二.sw ...

  9. mysql查询优化以及面试小结

    mysql面试小结: 1.mysql的基本架构 2.mysql的索引 btree+的原理 3.mysql的索引优化 4.mysql的sql查询优化 慢查询日志 Show prodile 全局查询日志 ...

  10. Poj3580 Super Memo(FHQ-Treap)

    题面 题解 对于操作$1$,我们可以对于每个节点打一个$add$标记,下放就行了 对于操作2,可以参考这篇题解的上一篇,不赘述 对于操作4,可以将区间裂成两部分,然后再插入合并 对于操作5,可以将区间 ...