Skip to content Skip to sidebar Skip to footer

Javascript Array Destructuring Assignment Gives Strange Errors

I've just noticed a strange error when using Javascript destructuring assignment, which took me some guesswork to resolve. I'm posting here so I can show what I learned. (I accep

Solution 1:

The problem here is a confusing ambiguity in Javascript syntax.

Notice that I wasn't using ; statement terminators?

It appears that the array destructuring assignment is being parsed as an array indexing operation applied to the previous statement.

Quick fix: add ; after the preceding statement (though this unfortunately forces an inconsistent style if these are generally omitted):

let next_col_time_step = head_steps.reduce(
    choose_next_step, [-1, -1, null]
    );
[next_step_col, next_step_time, next_step] = next_col_time_step

And, voila!, all is well :)

Post a Comment for "Javascript Array Destructuring Assignment Gives Strange Errors"