JS string destructuring: rest parameter returning inconsistent data

122 views Asked by At

Consider the following examples

An old project:

const [x, ...y] = "text";
console.log(x) // "t"
console.log(y) // "ext"

A new project based on CRA:

const [x, ...y] = "text";
console.log(x) // "t"
console.log(y) // ["e", "x", "t"]

I'm not sure why y is returning a string ("ext") for the old project, where it's an array of characters (["e", "x", "t"]) for the new project. Is it something to do with different JS versions?

Note: Both result were extracted after running webpack dev server.

1

There are 1 answers

0
barzin.A On BEST ANSWER

in babel website you can see that your code based on es2015-loose convert to this code so output of this code is same with your old project

"use strict";

var _text = "text",
    x = _text[0],
    y = _text.slice(1);

console.log(x); // "t"

console.log(y); // "ext"