Instagram | Twitter | LinkedIn
What is the output of the following code ?
Click here to see the correct answer and explanation 馃憖
| Correct Answer | Explanation |
|---|---|
| B | Always consider using rest operator as the last element, otherwise it will be thrown: SyntaxError: rest element may not have a trailing comma. We could use let [first, last, ...rest] = ["馃嵀", "馃巸", "馃懟", "馃崿"] and the result of the console.log(rest[0]) would be: 馃懟 |
Explanation based on 馃憠馃徏 Destructuring assignment - Assigning the rest of an array to a variable | MDN
Code:
let [first, ...rest, last] = ["馃嵀", "馃巸", "馃懟", "馃崿"]
console.log(rest[0])
驴Qu茅 imprime el siguiente c贸digo?
Haz click aqu铆 para ver la respuesta correcta y su explicaci贸n 馃憖
| Respuesta correcta | Explicaci贸n |
|---|---|
| B | Siempre considera usar el operador rest como 煤ltimo elemento, sino, se lanzar谩 un SyntaxError: el elemento rest no puede tener una coma al final. Podr铆amos usar let [first, last, ...rest] = ["馃嵀", "馃巸", "馃懟", "馃崿"] y para el console.log(rest[0]) obtendr铆amos como resultado: 馃懟 |
Explicaci贸n basada en 馃憠馃徏 La desestructuraci贸n - Asignar el resto de un arreglo a una variable | MDN
C贸digo:
let [first, ...rest, last] = ["馃嵀", "馃巸", "馃懟", "馃崿"]
console.log(rest[0])

