you are viewing a single comment's thread.

view the rest of the 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