In Python, and maybe in Javascript, the boolean or and and operators return one of the operands, instead of true or false.
- In Python, one of the operands is returned:
'' || 'hello' == 'hello' - In comparison, in PHP:
'' || 'hello' == true;
Now,
- How is this behavior of boolean operators called?
- Does this also work in Javascript in all browsers?
As Ignacio’s answer points out, these are coalescing operators.
||is the null coalescing operator,&&is the null-safe coalescing operator (link to follow, if I can find onesorry, I can’t find a link).They should be available in all browsers – they are both defined in the ECMA-262 1st, 2nd, 3rd and 5th editions, most current Javascript implementations are based upon 3rd or 5th. From ECMA-262 3rd edition: