Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

328
Vistas
Variable created by const is not accessable before initialization in javaScript. But why there is no error in react create functional component
const BasicFunctionalComponent = props => {
   return (
      <div>
        {constantVariable}
      </div>
   )
}

export default BasicFunctionalComponent

const constantVariable = '**how I am initialized**?'

in example above I just create a basic functional component and after exporting it just initialized a constant variable set as a string. then I used it inside JSX above expected to go throw error 'Cannot access 'constantVariable' before initialization'. but all's good

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Because BasicFunctionalComponent will get called much later. It's basically an asynchronous function from the perspective of this file you showed us. By the time that function is called, constantVariable will be defined.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Let's start with a much more clear example:

const fx = () => {
    console.log(v)
}

// fx() // Call it here and it throws error

const v = 1

// fx() // Call it here and it's ok

console.log('Fin!')

The reason behind why it does not throw ReferenceError is basically because the component function is not called before the initialization of the constantVariable.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda