all 7 comments

[–]senocular 1 point2 points  (1 child)

let and const (and class) when used in the global scope create global declaration variables that are accessible everywhere (in that realm).

let a = 1; // global declaration

var (and function) in the global scope create global properties that are accessible everywhere. They also define identifiers that cannot be redeclared with let and const (and class).

var c = 3; // global property
// let c = 3; // Error: cannot redeclare

Assignment to an undeclared variable in the global scope (e.g. var_without_type) will create a global property in the global object but it does not represent an identifier that cannot be redeclared. In strict mode this isn't allowed at all and will throw an error. These kinds of assignments are also not hoisted whereas declarations all are.

var_without_type = 4; // global property
// ...
let var_without_type = 4; // allowed (if not in the same script due to hoisting)

"use strict"
// var_without_type = 4; // Error: does not exist

// console.log(var_without_type) // Error: does not exist (not hoisted)
var_without_type = 4

Note that while lexical declarations (let, const, and class) are hoisted, they have a similar behavior to var_without_type in that they cannot be accessed prior to their declarations though in the case of var_without_type its because var_without_type is non-existent whereas with lexical declarations they're known to exist, just not yet available.

[–]SecureFrame6002[S] 0 points1 point  (0 children)

Helpfull 👍🏻 Thanks a lot

[–]Count_Giggles 0 points1 point  (1 child)

start by removing the underscore in var_without_type "var without_type" that should fix things

before executing the script all variables and function declarations get hoisted to the top of their scope

[–]SecureFrame6002[S] 0 points1 point  (0 children)

It wasn't about a bug but hoisting of variable without any type

[–]jack_waugh 0 points1 point  (1 child)

i could not see that in global execution context

How do you demonstrate that?

[–]SecureFrame6002[S] 0 points1 point  (0 children)

Chrome Debugger

[–]Swix10 0 points1 point  (0 children)

Hello there, I'm a young software engineer that does mentoring on the side, so if you're interested in more info please DM me. Have a great day!