# Execution order

**URL:** <https://es.discourse.group/t/execution-order/81>\
**Category:** Spec Reading\
**Created:** [July 9, 2019, 9:18pm UTC](https://es.discourse.group/t/execution-order/81 "2019-07-09T21:18:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Validark](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/validark/32/96_2.png) [@Validark](https://es.discourse.group/u/Validark)\
**Post date:** [July 9, 2019, 9:18pm UTC](https://es.discourse.group/t/execution-order/81/1 "2019-07-09T21:18:57Z")

</div>

I apologize for the obvious question, but the spec is a bit difficult for me to read and understand on this particular case. I am wondering in what order a hypothetical bit of code like this should execute:

```javascript
const { [g()]: x } = f();

```

Should `g` or `f` be called first? Out in the wild, it seems that Chrome, Node, Edge, and Babel all agree that `f` should be called first, then `g`. TypeScript, on the other hand, (and thus ts-node) thinks that `g` should be called first, then `f`. My personal feeling is that I think it makes more sense to call `g`, then `f`, just based on how the program looks. So, who is right? What does the spec have to say about which order this code should execute in?

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [July 9, 2019, 9:53pm UTC](https://es.discourse.group/t/execution-order/81/2 "2019-07-09T21:53:12Z")

</div>

Given that this is destructuring, and that the pattern on the LHS isn't even necessary unless `f()` is non-nullish, I'd assume `f` would be called first, and then `g`.

As for the spec, it seems to me like [https://tc39.es/ecma262/#sec-assignment-operators-runtime-semantics-evaluation](https://tc39.es/ecma262/#sec-assignment-operators-runtime-semantics-evaluation) step 3 evaluates the RHS first, and then step 5 evaluates the LHS - but I might be reading the wrong thing.

---

<div class="post-metadata">

**Author:** ![jmdyck](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jmdyck/32/796_2.png) [@jmdyck](https://es.discourse.group/u/jmdyck)\
**Post date:** [July 9, 2019, 10:12pm UTC](https://es.discourse.group/t/execution-order/81/3 "2019-07-09T22:12:53Z")

</div>

`const { [g()]: x } = f();`  
is what the spec calls a LexicalDeclaration. Evaluation of these is specified in [clause 13.3.1.4](https://tc39.es/ecma262/#sec-let-and-const-declarations-runtime-semantics-evaluation). For this code, it boils down to evaluating  
`{ [g()]: x } = f()`  
which in this context is an instance of the production

> LexicalBinding : BindingPattern Initializer

which is handled in the last block of that clause. There, step 1 evaluates the Initializer, and step 4 performs BindingInitialization of the BindingPattern, in which the ComputedProperty `[g()]` would be evaluated. So `f()` should be evaluated before `g()`.
