Explica este código JavaScript
const getName = (obj) => {
obj.name ??= "Sin Nombre";
return obj;
}
console.log(getName({}))
- A.
undefined
- B.
{}
- C.
{ name:"Sin Nombre" }
- D. Ninguno de los anteriores
Respuesta en el primer comentario.
For further actions, you may consider blocking this person and/or reporting abuse
JMcKeag-blip -
Magdalena Richard -
Kelechi Kizito Ugwu -
Sardar Mudassar Ali Khan -
Once suspended, duxtech will not be able to comment or publish posts until their suspension is removed.
Once unsuspended, duxtech will be able to comment and publish posts again.
Once unpublished, all posts by duxtech will become hidden and only accessible to themselves.
If duxtech is not suspended, they can still re-publish their posts from their dashboard.
Once unpublished, this post will become invisible to the public and only accessible to Cristian Fernando .
They can still re-publish the post if they are not suspended.
Thanks for keeping DEV Community safe. Here is what you can do to flag duxtech:
Unflagging duxtech will restore default visibility to their posts.
Top comments (1)
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 valornull
oundefined
.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 sonundefined
y por consecuencianullish
, por ello aobj.name
se el asigna el valor"Sin Nombre"
y retornamos ese objeto.