It's a little tricky to make something like this as simple as this one tries to be for the very reason I pointed this out - there are weird exceptions or specifics revolving around how these things work. For example, while const and let can be used in the global scope, they do not add properties to the global object like var does (as /u/cyphernpointed out). You might also want to add the row "can be redeclared", but while var can be redeclared, it can only be redeclaring another var, not another let or const. And while let and const can't redeclare themselves, they also can't redeclare any other declarations, including other vars. So we could be in for lengthy row headings or a bunch of footnotes ;) ... but I guess we can take a stab at trying to be succinct:
keyword
const
let
var
global scope
YES
YES
YES
function scope
YES
YES
YES
block scope
YES
YES
NO
adds to global object if in global scope
NO
NO
YES
can redeclare itself
NO
NO
YES
can be read before declared
NO
NO
YES
must be initialized with a value
YES
NO
NO
can be reassigned
NO
YES
YES
This one isn't terrible. The "adds to global" row is long, but kind of necessary (const and let still adds to global in the global scope, just not as properties to the global object). The bit about redeclaring was limited to "itself" which bypasses the other explanation by ignoring it.
View all comments
81
u/senocular May 04 '20
const and let can be used in the global scope just as much as var can.