DEV Community

Cristian Fernando
Cristian Fernando

Posted on

2 2

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

Explica este código JavaScript

const getName = (obj) => {
  obj.name ??= "Sin Nombre";
  return obj;
}
console.log(getName({}))
Enter fullscreen mode Exit fullscreen mode
  • A. undefined
  • B. {}
  • C. { name:"Sin Nombre" }
  • D. Ninguno de los anteriores

Respuesta en el primer comentario.


Top comments (1)

Collapse
 
duxtech profile image
Cristian Fernando

Respuesta:
C. { name:"Sin Nombre" }

El operador ??= se llama Logical Nullish Assignment es un operador de corto circuito moderno que consiste en ejecutar porciones de código si evaluamos una condición como nullish, osea, como valor null o undefined.

Entonces, en el ejemplo, si obj.name evalua como nullish, ejecutamos "Sin Nombre".

Llamamos a la función getName pasandole un objeto vacío, entonces todas sus propiedades son undefined y por consecuencia nullish, por ello a obj.name se el asigna el valor "Sin Nombre" y retornamos ese objeto.

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

If you found this post useful, consider leaving a ❤️ or a nice comment!

Got it