var declares a variable that is an entire region
Let declares that the variable is valid only in the local area.
123<script>4 functionMyFunction1 () {5 varA=1;6 if(1){7 vara=2;8Console.log (a);//Output 29 }TenConsole.log (a);//Output 2 One } A functionMyfunction2 () { -Let A=1; - if(1){ theLet a=2; -Console.log (a);//Output 2 - } -Console.log (a);//Output 1 + } -</script> + A<body> at<H1 id= "H1" > - the difference between Var and let - -<p id= "P1" > - let allows you to declare a variable, statement, or expression that has a scope that is limited to the block level. The Let variable is recommended for local variables in function to avoid variable name collisions. -</p> in<button type= "button1" onclick= "MyFunction1 ()" > Functions 1 Execution </button> -<button type= "Button2" onclick= "Myfunction2 ()" > Functions 2 Execution </button> to +</body> -From the output, we can see that the VAR declaration variable A can change his value in the If.
The variable a=1, declared in the IF, a=2 is two separate variables.
JavaScript variable declares the difference between Var and let