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

107
Views
Different result of JavaScript scope chain in the Chrome browser

I have a question about mechanism of making scope chain in JavaScript. In the Chrome browser, the results of scope chain is different according to refer variables or not.

Let me show you an example.

EXAMPLE 1. CODE AND RESULT

var A = 'Hello'

function foo() {
    var B = 'World'
    function bar() {
        var A = 'Junhyunny'
        if (A) {
            const C = 'JavaScript'
            if (B) {
                const D = 'Post'
                function baz() {
                    console.log(A)
                    console.log(B)
                    console.log(C)
                    console.log(D)
                    console.dir(baz)
                }
            }
        }
        baz()
    }
    bar()
}

foo()
  • There are five scopes in [[Scope]], I expected.

enter image description here

EXAMPLE 2. CODE AND RESULT

var A = 'Hello'

function foo() {
    var B = 'World'
    function bar() {
        var A = 'Junhyunny'
        if (A) {
            const C = 'JavaScript'
            if (B) {
                const D = 'Post'
                function baz() {
                    // console.log(A)
                    // console.log(B)
                    // console.log(C)
                    // console.log(D)
                    console.dir(baz)
                }
            }
        }
        baz()
    }
    bar()
}

foo()
  • There are only 3 scopes in [[Scope]].
  • I guess the reasons for this result.
    • Declaration for function baz is effected by variable B, so scope Block(foo) is necessary.
    • Variable B is declared in function foo, so scope Closure(foo) is necessary.
    • Global Scope is necessary.

enter image description here

Is there some optimizing logic to make scope chain? I cannot found references for this phenomenon.

about 4 years ago · Juan Pablo Isaza
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!