Champer Wu Champer Wu - 1 year ago 77
Javascript Question

Why are using spread operator at CODEPEN and chrome have different result?

I tested the spread operator at CODEPEN and chrome and I got different result

var str = 'foo';
var char = [...str];

At CODEPEN I use preprocessor is Babel and I got

At Chrome developer tools, I got
["f", "o", "o"]

Why is it happened?

Answer Source

As mentioned in the comment, this is related to babel js transpiler. Looks like codepen is using this babel-preset es2015-loose and it has some divergences in its spread operator implementation:

Babel’s loose mode transpiles ES6 code to ES5 code that is less faithful to ES6 semantics.


This is actually a codepen issue, they probably shouldn't be using loose mode these days.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download