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

163
Views
block scope influencing declarations outside of it

<p id="Test1"></p>
<p id="Test2"></p>
<p id="Test3"></p>

<script>
    {
        let v;
        v = 10;
    }

    let g = 20;

    var h = 15;

    document.getElementById('Test1').innerHTML = v;
    document.getElementById('Test2').innerHTML = g;
    document.getElementById('Test3').innerHTML = h;
</script>

As far as I learned, the let declared variables, can't be used outside a blocked scope, but if I run the code above, none of the 3 get shown? why is that? the scope ended there right?

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

0

When an error occurs, JavaScript will normally stop and generate an error message. The technical term for this is: JavaScript will throw an exception (throw an error). JavaScript will actually create an Error object with two properties: name and message. **SO HERE AS SOON AS COMPILER TRY TO SHOW "v" js compiler throw error **

  • Uncaught ReferenceError: v is not defined And it terminate execution due to which you can not see that value of g and , h. here are two case: ** case 1** AS "v" IS COMMENTED YOU CAN SEE VALUE OF g and h(without error in console).
    let g = 20;
    
    var h = 15; {
    
      let v;
      v = 10;
    }
    
    
    //document.getElementById('Test1').innerHTML = v;
    document.getElementById('Test2').innerHTML = g;
    document.getElementById('Test3').innerHTML = h;
    <p id="Test1"></p>
    <p id="Test2"></p>
    <p id="Test3"></p>

** CASE 2** ** I TRIED TO ACCESS v VALUE AS LAST SO NOW YOU CAN SEE VALUE OF g and h as IT WAS EXECUTED EARLIER BEFORE THE ERROR THROWN BY COMPILER.

let g = 20;

var h = 15; {

  let v;
  v = 10;
}



document.getElementById('Test2').innerHTML = g;
document.getElementById('Test3').innerHTML = h;
document.getElementById('Test1').innerHTML = v;
<p id="Test1"></p>
<p id="Test2"></p>
<p id="Test3"></p>

JavaScript is a single-threaded language, where only one command executes at a time. It has a Synchronous model of execution, where each line is executed line by line, top to bottom.

UNIT AND NLESS THERE IS COME FUNCTION SUCH AS SET TIMEOUT OR ASYNCHRONIZED FUNCTION. WHICH EXECUES DIFFERENTLY.

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!