Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

321
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!