In below example, there are 2 variables with same name but they will be scooped differently as one of them is defined using var keyword and other is defined using let keyword.
Scenario 1: Valid test case
You can redeclare a variable in a child block
<script type="text/javascript"> function variableHoisting(i){ var a =3; //function scoped if ( i == 1){ let a = 3;//local variable to function. Should not be accessible outside this block i.e. { } } console.log(a);//should access function scoped a so output will be always 3 } variableHoisting(1); variableHoisting(2); //Call method to check output </script>
style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-5021110436373536"
data-ad-slot="9215486331">
Output:
3
3
Scenario 2: Invalid Test case
You cannot redeclare a variable again using let keyword in same block.
<script type="text/javascript"> function variableHoisting(i){ var a =3; //function scoped let a =3; // block scoped. But here block scope is fuction only so conflicting declaration } </script>
Output:
Error – SCRIPT1052: Let/Const redeclaration