Created by ThinkingStiffLinked to 70.3m issues across 111 teams
The main difference between let
and var
is the scope in which they are defined. var
is a keyword used to declare a variable in the global scope, meaning it can be accessed from anywhere in the code. let
is a keyword used to declare a variable in the local scope, meaning it can only be accessed within the block of code in which it is declared.
For example:
var x = 10; function myFunction() { let y = 20; console.log(x); // prints 10 console.log(y); // prints 20 } myFunction(); console.log(x); // prints 10 console.log(y); // throws an error
In the example above, x
is declared using var
and is accessible both inside and outside of the myFunction
function. y
is declared using let
and is only accessible within the myFunction
function.
Contributor
Teams
111