In most other programming languages, the final print statement will always print 5 because most programming languages use block level scope. In other words, the variable assignment in the conditional has no impact on the code outside the conditional block.
Luckily, this can be avoided with better name choice, and by declaring all variables at the top of functions.
Interestingly, function scoped variables are “hoisted” and thus always available in some form throughout the function. Therefore, declaring variables at the top of functions is most in line with how the code is processed by a computer.
The difference between the keywords is reassignment. Variables declared with const cannot be reassigned to another variable. On the other hand, variables declared with let have that ability.
It should also be noted that variable hoisting that was mentioned earlier does not apply to variables declared with const and let. Therefore, if one tries to access a variable declared with const or let before it has been declared, then an error will be raised.