0

Hello everyone my question is do vars and functions get stored in a block where they inside the block ?

consider the following code

function myFunction() {
  {
    var a = 5;

    function sayA() {
    }
  }
}

So again will function sayA and var a get stored in the block variable environment or the function variable environment?

I also made a picture to help you understand what I exactly mean enter image description here

you may ask yourself why i put in the picture sayA and a variable inside myFunction because var and sayA are function scoped so myFunction will have a reference to the memory address of sayA and a

but sayA and a variable are really stored in the memory space of Block 1 right ? and because they considered a function scoped the myFunction execution context have a reference to their memory address

right ?

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • sounds right... – 0xLogN Apr 07 '21 at 15:03
  • `var` is not block-scoped. Functions...are weird with web compat rules. You'll probably get a better answer if you have a more concrete problem. – VLAZ Apr 07 '21 at 15:04
  • 1
    Memory management is an implementation detail of the JS engine, the language itself does not care about it. – Quentin Apr 07 '21 at 15:05
  • No. `var` and `function` declarations are scoped to the enclosing function scope, not the local block scope. – Pointy Apr 07 '21 at 15:05
  • there is a difference between scope and where variables lives my question is do var and the function lives inside the block 1 memory space ? – CaptainMagmelaton Cap Apr 07 '21 at 15:05
  • Both the `var` and the `function` can be referenced from outside the block. They are hoisted to the enclosing function scope. – Pointy Apr 07 '21 at 15:06
  • 1
    There is no "memory space" for blocks. – VLAZ Apr 07 '21 at 15:07
  • @VLAZ try to create a function inside a block and in that block create also a let variable and outside of the block in the function create a variable of type var with the same name of the let variable and do console.log in the function inside the block and see that it prints the let variable because the function outer reference is the block memory space and not the function BUT you can still access that function outside of the block because it's function scoped – CaptainMagmelaton Cap Apr 07 '21 at 15:18
  • @CaptainMagmelatonCap why should I do any of this? Also, you're way *way* over-simplifying things - strict mode *does* have block level functions. Trying `{function foo() {}} foo()` in strict mode throws an error. Even in loose mode, the interaction is not straight forward because of the semantics for legacy compatibility. For example `foo(); {function foo() {}}` will throw an error. – VLAZ Apr 07 '21 at 15:26
  • @VLAZ that's because when you type 'use strict' the function will no longer be scoped to the function but it will be to the block. and by the way the code `foo(); {function foo() {}}` will throw an error because the function is inside the block and javascript will not create that function until it gets to the block basically you're doing undefined(); during the creation phase it wont create the function but it will set `foo` to undefined that's in case the function is in a block. why? for example `if(false){function foo(){}}` why Javascript need to create a function that will never get used ? – CaptainMagmelaton Cap Apr 07 '21 at 15:41
  • there are no "weird function things" – CaptainMagmelaton Cap Apr 07 '21 at 15:42
  • "*there are no "weird function things"*" [If you say so](https://stackoverflow.com/q/66756997) – VLAZ Apr 07 '21 at 15:43
  • "Usually a Lexical Environment is associated with some specific syntactic structure of ECMAScript code such as a FunctionDeclaration, a BlockStatement, or a Catch clause of a TryStatement and a new Lexical Environment is created each time such code is evaluated. An Environment Record records the identifier bindings that are created within the scope of its associated Lexical Environment. It is referred to as the Lexical Environment's EnvironmentRecord" – CaptainMagmelaton Cap Apr 07 '21 at 16:49

0 Answers0