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

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

Ansible warning on jinja2 braces on when -