Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6757649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:40:42+00:00 2026-05-26T13:40:42+00:00

The ECMAScript 5 spec states the following: Usually a Lexical Environment is associated with

  • 0

The ECMAScript 5 spec states the following:

Usually a Lexical Environment is associated with some specific
syntactic structure of ECMAScript code such as a FunctionDeclaration,
a WithStatement, or a Catch clause of a TryStatement and a new Lexical
Environment is created each time such code is evaluated.

If my understanding is correct, then when a new Lexical Environment is created in JavaScript, a new scope is entered, which is why variables declared inside a function are not visible outside of that function:

function example() {
    var x = 10;
    console.log(x); //10
}
console.log(x); //ReferenceError

So in the above function declaration, a new Lexical Environment is created, which means x is not available in any outer Lexical Environments that may exist.

So the part of the quote above about Function Declarations seems to make sense. However, it also states that a new Lexical Environment is created for the Catch clause of a Try Statement:

try {
    console.log(y); //ReferenceError so we enter catch
} 
catch(e) {
    var x = 10;
    console.log(x); //10
}
console.log(x); //10 - but why is x in scope?

So how does the scope of a catch block work? Do I have a fundamental misunderstanding of what a Lexical Environment is?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:40:43+00:00Added an answer on May 26, 2026 at 1:40 pm

    If I understand it right, then what it probably means is that, in your code,

    try {
        console.log(y); //ReferenceError so we enter catch
    } 
    catch(e) {
        var x = 10;
        console.log(x); //10
    }
    

    e will only exist in the catch block. Try console.log(e); outside the catch block and it will throw ReferenceError.

    As with WithStatement, with ({x: 1, y: 2}) { }, x and y will only exist inside the with block.

    But it doesn’t mean that var declarations will be bound to the closest lexical environment. Actually, var declarations will be bound to the environment when entering an execution context.

    10.5 Declaration Binding Instantiation: when the execution context in being entered, it will look for function declarations, arguments, and variable declarations and then create bindings in the execution context’s VariableEnvironment.

    So any variables that are declared using var will be accessible anywhere in the function regardless of the control structures or where it is defined inside the function. Note that this does not include nested functions as they’re a separate execution context.

    That means the var declarations will be bound to the closest execution context.

    var x = 1;
    (function() {
        x = 5; console.log(x); // 5
        if (false) { var x; }
        x = 9; console.log(x); // 9
    })();
    console.log(x); // 1
    

    So in above code, x = 5; will set the x variable inside the inner function, because var x; inside if (false) { var x; } is bound to that function already before the function code is executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some ECMAScript/JavaScript for an SVG file and need to get the
I've been working on my own implementation of ECMAScript for quite some time now.
Although Ecmascript 5 introduces some completely new features, it also adds some new methods
I have been playing around with Object.create in the EcmaScript 5 spec, and I
I am wanting to experiment with some of the new ECMAScript 5 features. I
I'm 99.9% positive that this isn't possible, but there may be some obscure ecmascript
I'm looking for a VM with the following features: Small compiled code footprint (
I've seen some proposals for ECMAScript Harmony in terms of being able to specify
Servus Ninjas, this is what ecmascript 5 spec. (page 118) in the section Function.prototype.apply(thisArg,
Does the ECMAscript specification impose a specific pseudo-random generating algorithm?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.