Explica este código JavaScript
let name = 'Lydia'
function getName() {
console.log(name)
let name = 'Sarah'
}
getName()
- A:
Lydia
- B:
Sarah
- C:
undefined
- D:
ReferenceError
Respuesta en el primer comentario.
For further actions, you may consider blocking this person and/or reporting abuse
duncan -
Riccardo Tartaglia -
Shivam Singh -
Hezekiah -
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:
D:
ReferenceError
Las variables declaradas con
let
yconst
tienen scope de bloque es por este motivo que si bien tenemos 2 variables con el nombrename
, ambas son diferentes e independientes en sus respectivos scopes.La función
getName
intenta imprimir por consolaname
antes de ser declarada, por hoisting el interprete de javascript hará quename
entre en lo que se denomina Temporal Dead Zone, una región del código donde la variable esta declarada pero no es posible acceder a ella.Todo esto producirá un
ReferenceError
.Si dentro de la función
getName
la variablename
estuviera declara convar
:Por hoisting el resultado seria
undefined
puesto que la Temporal Dead Zone solo existe con variables declaradas conlet
yconst
.