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

174
Views
Newbie Questions on Scoping and Hoisting?

I am learning Javascript and I got into the hoisting/scoping part. I got some questions that I really hope somebody can help me to clarify and here is my example:

So these works normally due to hoisting - the firstName variable is in Global Scope or function can be called before we declare the function:

function test() {
  console.log(firstName);
}
const firstName = 'a';
test();

OR

const firstName ='a';
test();
function test (){
  console.log(firstName)
}

However, if I swap the order of firstName and test(), the code does not work anymore:

function test() {
  console.log(`${firstName}`)
};
test();
const firstName = 'a';

According to the hoisting, I thought the firstName variable is in the Global Scope. Thereby, the test function should have been able to look up at the variable firstName when it is executed.

But why in the case above, it does not work?

Is there anything wrong with my understanding of hoisting/scoping?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Variables declared with let and const are also hoisted, but unlike for var the variables are not initialized with a default value of undefined. Until the line in which they are initialized is executed, any code that accesses these variables will throw an exception.

MDN DOCS

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!