1

Per this link, we know

console.log(false ** false == true);  // true
console.log(false ** true == false);  // true
console.log(true ** false == true);  // true
console.log(true ** true == true);  // true

I think we can implement Converse implication through exponentiation operator in ES7.

Given the return value of function f() and g() are boolean. So we can use this simple codes

if (g() ** f()) { 
   // 
}

to replace

if ((g() && f()) || !f()) {
    //
}

Some test codes are here. Is it valid or not under ES7?

Community
  • 1
  • 1
zangw
  • 43,869
  • 19
  • 177
  • 214
  • 1
    Even if it's valid, what's the meaning to use that? I can't see that it make the expression more efficient, nor clearer. – fuyushimoya Nov 23 '15 at 10:10
  • 1
    Use `g() || !f()` instead, it's much more readable. Try: `[[false, false], [false, true], [true, false], [true, true]].map(function(a) { return a[0] || !a[1]; })` – Amit Nov 23 '15 at 10:14

2 Answers2

2

The statement:

if ((g() && f()) || !f()) {

Has several potentially bad implications, resulting from the short-circuit mechanism:

  1. if g() is falsy, f() is evaluated once, and the negated result is used to branch.
  2. if g() is truthy, and the first f() is also truthy, the content of the if block is evaluated.
  3. if g() is truthy, and the first f() is falsy, f() is evaluated again - which might be a costly operation and might return a completely different value, which is then negated and used to branch.

The statement:

if (g() ** f())

results in both functions evaluated exactly once, and the results are then logically combined to branch.

Depending on required side effects and / or evaluation order, you should either use:

let gVal = g(), fVal = f();
if(gVal || !fVal) { ... }

Or

if(g() || !f()) { ... }

Having said all that, to answer the question itself... ECMAScript 2016 is not finalized yet, but your assumptions seem correct.

Amit
  • 45,440
  • 9
  • 78
  • 110
1

Yes, Exponentiation can be used for converse implication in ES6.

You can verify this here

zahreelay
  • 1,742
  • 12
  • 18