1

I recently stumbled upon the new Destructuring Javascript feature that is available with ES6.

Found out a weird situation where i am not really sure of what is going on. Hopefully you guys will help me understand.

If i type this in my console:

var car={}
var {undefinedProp: es6Magic} = car;

I get an undefined. Looks fair to me, since car has no defined property.

But if i use an If statement around it i get a different and unexpected result:

function testPriorities() {
      var car = {}
      if ({
          undefinedProp: es6Magic
        } = car) {
        console.log('how do i even get here', es6Magic);
      }
    }

What the hell?

Why is that es6Magic is assigned with an undefined value and it still returns true?

What rules are being applied when running the if statement?

Tiago Bértolo
  • 3,874
  • 3
  • 35
  • 53
  • 2
    `({undefinedProp: es6Magic} = car)` returns an empty object `Object {}`. So, it's not false.. – choz Jun 29 '17 at 16:44
  • you can't do destructuring in that way I'm afraid. Other than the fact that you're assigning rather than checking, the first part of the check is being parsed as an object, and not as syntactic sugar that is destructuring – towc Jun 29 '17 at 16:44

3 Answers3

3

If i type this in my console:

var car={}
var {undefinedProp: es6Magic} = car;

I get an undefined.

But not because es6Magic has an undefined value (it does, I mean it's not the reason). It's because variable declarations have no result value, and your complete snippet does not have a result (unlike expression statements).

But if i use an If statement around it i get a different and unexpected result:

var car = {}
if ({undefinedProp: es6Magic} = car) {
    console.log('how do i even get here', es6Magic);
}

I guess that is true because the car exists, but why does it evaluate differently from the console?

Actually you'll still get the undefined result from the last statement, after the console.log output.

And yes, the if condition evaluates to a truthy value because car exists - that's what assignment expressions always do. This doesn't even have anything to do with destructuring, … = car always evaluates to the right hand side car regardless what the left hand side target expression is.

You can also try

> var car = {}, es6Magic;
undefined
> ({undefinedProp: es6Magic} = car); // no `var` - plain assignment!
[object Object]
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

The if statement executes because you are basically doing assignment inside if statement which always return the assigned value which in this case is {} which evaluates to true.

var b

if(b={}){console.log("what!!")}

function a(){
  var car={}
  return {undefinedProp: es6Magic} = car;
}

console.log(a())
Anurag Awasthi
  • 6,115
  • 2
  • 18
  • 32
1

The console shows undefined because a variable declaration doesn't return anything, it declares a variable.

The second version works because {foo: bar} is interpreted as an object literal, which you are assigning to. That returns the object, which is truthy. I would expect that to throw an error, which it does in the console:

Uncaught SyntaxError: Invalid destructuring assignment target

The literal shouldn't be a valid target, but a transpiler would most likely break that.

ssube
  • 47,010
  • 7
  • 103
  • 140