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

125
Views
Javascript hoisting and scoping
function sayHi(name){
   let result="Hi"+name+a;
   console.log(result)
}

let a=1;
sayHi("Alex")

The result of the above code is Hi Alex1, so from what I understand is the variable a is hoisted, but in JS, only the declaration is hoisted, not the initialisation, so how come the result can use the value of a?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Because the value is set before the operation is called. Look at the order of these two things:

let a=1;
sayHi("Alex")

First the value of a is set to 1, then the function sayHi is invoked. Within that function the value of a is used.

Swap the two statements and see a different result:

function sayHi(name){
   let result="Hi"+name+a;
   console.log(result)
}

sayHi("Alex")
let a=1;

about 4 years ago · Santiago Trujillo Report

0

Functions don't try to access variables until they are called, which happens after the a variable has had a value assigned to it.

about 4 years ago · Santiago Trujillo Report

0

Javascript works like a waterfall. This means code will be compiled from the higher level. The variable a is initialized before the function sayHi is called: so you have access to his value. Check how scope works as well here https://www.w3schools.com/js/js_scope.asp

about 4 years ago · Santiago Trujillo 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!