Difference between var & let

 Below examples will helps us to understand the major difference between "var" & "let".

Let: Any variables declared this way are in the closest block scope (meaning within the {} they were declared in).

function scope3(print)
{
	if(print)
	{
		let insideIf = '12';
	}
	console.log(insideIf);
}
scope3(true); // Output: It will error out.
scope3(print)
{
	if(print)
	{
		var insideIf = '12';
	}
	console.log(insideIf);
}
scope3(true); // Output:  12

Note:
var declares the variable within the function scope, let declares the variable
in the block scope, and variables can be declared without any operator in the global
scope; however, the global scope should be avoided at all times.

Comments