DEV Community

Cristian Fernando
Cristian Fernando

Posted on

1

Paracetamol.js💊| #192: Explica este código JavaScript

Explica este código JavaScript

Dificultad: Básico

const x = [1,2,3];
const y = x.concat();
x.push(4)
console.log(y)
Enter fullscreen mode Exit fullscreen mode

A. [1, 2, 3]
B. []
C. [4]
D. [1, 2, 3, 4]

Respuesta en el primer comentario.


Respuesta:
A. [1, 2, 3]

El método concat es inmutable, por ende siempre regresará un nuevo arreglo como resultado y no modificará el existente.

En este caso, concat no recibe ningún parámetro y esto conlleva a que y sea una copia de x, no de su referencia, sino de sus valores.

y al ser una copia no se ve afectado por el push que se le hace a x.


Esta es una manera un poco rara de crear copias de arreglos, no es muy intuitiva y no la recomiendo usar, para llegar a lo mismo podríamos emplear el spread operator:

const x = [1,2,3];
const y = [...x];
x.push(4)
console.log(y)
Enter fullscreen mode Exit fullscreen mode

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 🕒

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay