+ 1

Js: `${...arr}`

Where is the error in this quiz? var arr = [2,3,4]; var lit = `${...arr}`; console.log(lit[0];

20th Jun 2020, 12:25 PM
Prof. Dr. ZoltĂĄn Vass
6 Answers
+ 3
https://code.sololearn.com/Wma3zSx6o1i6/?ref=app You just need to use the array destructuring syntax: var lit = [...arr];
20th Jun 2020, 12:47 PM
Ali Abdelhady
Ali Abdelhady - avatar
+ 2
Ali Abdelhady Thanks! With a minor modification of your example, it works: var arr = [1, 2, 3, 4]; var lit = [...arr]; console.log(lit[0]);
20th Jun 2020, 1:00 PM
Prof. Dr. ZoltĂĄn Vass
+ 2
var arr = [2,3,4]; var lit = `${[...arr]}`; console.log(lit[0]); Use [] inside ${} so that lit variable makes a copy of arr variable
20th Jun 2020, 2:43 PM
Coder
Coder - avatar
+ 1
Prof. Dr. ZoltĂĄn Vass well it does work as well, by using the same syntax inside the {}: `${[...arr]}` https://code.sololearn.com/Wma3zSx6o1i6/?ref=app
20th Jun 2020, 1:10 PM
Ali Abdelhady
Ali Abdelhady - avatar
0
Mirielle The error is in this line: var lit = `${...arr}`; I still don’t understand why. Perhaps destructuring can’t be used in string literals?
20th Jun 2020, 1:06 PM
Prof. Dr. ZoltĂĄn Vass