Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: implement let-working with var

So I have a code that clone let-behavior. But I don't undestand how and why it is working. Can someone explain it?

(function() {
  var a = 2;
})()
like image 429
Spawni Avatar asked Dec 22 '22 19:12

Spawni


1 Answers

let is scoped to the block it appears in.

var is scoped to the function it appears in.

By replacing a block with a function (which is immediately invoked) var is scoped to the same lines of code as let would be.

like image 83
Quentin Avatar answered Jan 03 '23 02:01

Quentin