04:49
<Tushar Vaswani>

Hey guys I was reading about environment records and lexical environment and I got a confusion. Don't know if this is the right channel to discuss about this. But basically my question is this:

If we have this code:

{
    const x = 2;
    var y = 3;
}

console.log(x); // will fail
console.log(y); // will work fine

Then here x was part of lexical environment(and ultimately environment record) of the block. That's why it will not be accessible outside the block.

But var is accessible becasue it's not block scoped. So does that mean it's not stored in environment record of block and stored directly in global environment record (and global object basically?) or in both global environment record and environment record of block.

04:51
<rkirsling>
yeah, var effectively "doesn't see" {}
04:53
<Tushar Vaswani>
yeah, var effectively "doesn't see" {}
Thanks for help, so that basically means it will be be stored in global environment record only?
04:54
<Tushar Vaswani>
I was mainly confused because its not clearly outlined in spec
04:55
<rkirsling>
yeah, "lexical" refers to the {}, let, const mechanism
04:55
<rkirsling>
var just knows about function scopes and global scope
04:55
<Jessidhia>
that depends on strict mode and on whether it’s inside or outside a function body
04:56
<Jessidhia>
it won’t be global on a strict mode script… IIRC
04:58
<Tushar Vaswani>
var just knows about function scopes and global scope
Thanks that makes a lot more sense now
05:01
<Tushar Vaswani>
it won’t be global on a strict mode script… IIRC
so does that mean we wont be able to access var variables outside block in strict mode?
05:02
<Tushar Vaswani>

Well actually maybe I misunderstood it because I just tried it and it seems to work still:

"use strict";
{
    var a = 1;
    console.log(a);
}
console.log(a);
05:03
<rkirsling>
you'd want to check globalThis.a but I do think it works, yeah
05:04
<Tushar Vaswani>
you'd want to check globalThis.a but I do think it works, yeah
yeah even that is printing the value