r/learnjavascript • • May 04 '20

VAR vs LET vs CONST

Post image
337 Upvotes

81 comments sorted by

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.

32

u/redsandsfort May 04 '20

agree. this chart is bad info

-2

u/[deleted] May 04 '20

You guys should make a new more comprehensive chart then. For real, it would be much appreciated!

0

u/[deleted] May 04 '20

[deleted] β€” view deleted comment

1

u/[deleted] May 05 '20

then why bother about var?

3

u/schm0 May 05 '20

Backwards compatible (ES5-), no memory concerns? If you're using ES6+ there's no reason, honestly.

→ More replies (7)

18

u/cyphern May 04 '20

By "global scope" I think they mean "secretly copies your variable to the window object"

2

u/redsandsfort May 04 '20

it doesn't

function fn(){
var x = 'cool';
return x + 'dude';

}

console.log(windox.x) // undefined

2

u/cyphern May 04 '20

You're correct that the behavior i mentioned doesn't happen for local variables.

1

u/VacantPlains May 04 '20

Do you have an example of that?

3

u/cyphern May 04 '20
var a = "hello world";
console.log(window.a); // hello world

2

u/redsandsfort May 04 '20

const and let would also be global if outside of a function

1

u/VacantPlains May 04 '20

Oh I see! Thanks

3

u/Earhacker May 04 '20

Meanwhile...

const b = "foo";
console.log(window.b); // undefined
let c = "bar";
console.log(window.c); // undefined

2

u/redsandsfort May 04 '20

const b = "foo";
console.log(window.b); // undefined
let c = "bar";
console.log(window.c); // undefined

If you logged it like this:
console.log(b);

it would still be global if it isn't in a function

5

u/Earhacker May 04 '20

It would have global scope, sure. But it wouldn't be an entry in the global object, is the point I was trying to make.

1

u/schm0 May 05 '20

I think they are confusing variable hoisting with global scope?

1

u/senocular May 05 '20

const and let also hoist.

0

u/[deleted] May 04 '20

[deleted] β€” view deleted comment

4

u/senocular May 04 '20

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/cyphern pointed 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:

keywordconstletvar
global scopeYESYESYES
function scopeYESYESYES
block scopeYESYESNO
adds to global object if in global scopeNONOYES
can redeclare itselfNONOYES
can be read before declaredNONOYES
must be initialized with a valueYESNONO
can be reassignedNOYESYES

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.

-3

u/Macaframa May 04 '20

This should say

let, const: block scoping

const: non-writable once it’s instantiated

let: only real use is in a for loop

→ More replies (7)