javascript - Left hand side expression increments even though exception is thrown -


given following code snippet:

const arr = [];  let pos = 0;    const fn = () => { throw 'foo'; };    try {      arr[pos++] = fn();  } catch (e) {      console.log(pos);  }

i surprised see value of pos 1. intuitively, have expected rhs evaluated first, when exception thrown pos remain unmodified.

could point relevant portions of spec covers this? or otherwise explain the behaviour?

ecmascript 2015 defines behavior:

assignmentexpression[in, yield] : lefthandsideexpression[?yield] = assignmentexpression[?in, ?yield]

  1. if lefthandsideexpression neither objectliteral nor arrayliteral, then

    a. let lref result of evaluating lefthandsideexpression.

    b. returnifabrupt(lref).

    c. let rref result of evaluating assignmentexpression.

    [...]

you can see left hand side expression evaluated first, right hand side expression (assignmentexpression).


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -