Explica este código JavaScript
function sum(num1, num2 = num1) {
console.log(num1 + num2)
}
sum(10)
- A:
NaN
- B:
20
- C:
ReferenceError
- D:
undefined
Respuesta en el primer cometario.
For further actions, you may consider blocking this person and/or reporting abuse
Kera Cudmore -
Sardar Mudassar Ali Khan -
Elliot Brenya sarfo -
Nitin Sharma -
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:
B:
20
Desde ES6 es posible usar parámetros por defecto (siempre y cuando sean los últimos declarados en la función).
En este caso el parámetro por defecto
num1
es el mismo que el primer parámetro, no hay ningun problema simpre y cuando este declarado al final de la lista de parámetros de la función.Pasamos el argumento
10
a la funciónsum
, esto significa quenum2
deberá usar su valor por defecto que seria el mismo denum1
, osea10
; entonces10 + 10
nos da el resultado final20
.