DEV Community

Cristian Fernando
Cristian Fernando

Posted on

3 3

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

Explica este código JavaScript

const set = new Set()

set.add(1)
set.add("Lydia")
set.add({ name: "Lydia" })

for (let item of set) {
  console.log(item + 2)
}
Enter fullscreen mode Exit fullscreen mode
  • A: 3, NaN, NaN
  • B: 3, 7, NaN
  • C: 3, Lydia2, [Object object]2
  • D: "12", Lydia2, [Object object]2

Respuesta en el primer comentario.


Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando

Respuesta:
C: 3, Lydia2, [Object object]2

A cada item del Set aplicamos el operador + con el número 2.

Para 1 que es number realizamos una suma simple obteniendo como resultado 3.

Para la cadena Lydia y por coerción de tipos convertimos al número 2 en string y realizamos una concatenación obteniendo Lydia2.

Para el objeto { name: "Lydia" } nuevemente por coerción de tipos convertimos tanto al objeto y al número 2 a string obteniendo [Object object]2.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay