Skip to content

Instantly share code, notes, and snippets.

@Chema22R
Created March 20, 2020 07:38
Show Gist options
  • Save Chema22R/4642b77e84bdeca0320acffac3542095 to your computer and use it in GitHub Desktop.
Save Chema22R/4642b77e84bdeca0320acffac3542095 to your computer and use it in GitHub Desktop.
Facing JS6 var, let, const

JS Compiler

Functions scope: var (limited), let (limited), const (limited)

function test() {
	var var_function = true;
	let let_function = true;
	const const_function = true;
}

console.log("var_function: " + typeof var_function);		// NO
console.log("let_function: " + typeof let_function);		// NO
console.log("const_function: " + typeof const_function);	// NO

JS blocks scope: var (allowed), let (limited), const (limited)

{
	var var_block = true;
	let let_block = true;
	const const_block = true;
}

console.log("var_block: " + typeof var_block);		// YES
console.log("let_block: " + typeof let_block);		// NO
console.log("const_block: " + typeof const_block);	// NO
if (true) {
	var var_if = true;
	let let_if = true;
	const const_if = true;
}

console.log("var_if: " + typeof var_if);	// YES
console.log("let_if: " + typeof let_if);	// NO
console.log("const_if: " + typeof const_if);	// NO
for (var var_for; !var_for;) {
	var_for = true;
}

for (let let_for; !let_for;) {
	let_for = true;
}

console.log("var_for: " + typeof var_for);	// YES
console.log("let_for: " + typeof let_for);	// NO
var t=false;
while (!t) {
	var var_while = true;
	let let_while = true;
	const const_while = true;
	t=true;
}

console.log("var_while: " + typeof var_while);		// YES
console.log("let_while: " + typeof let_while);		// NO
console.log("const_while: " + typeof const_while);	// NO

Use before declaration (hoisting): var (allowed), let (limited), const (limited)

var_hoist = true;
var var_hoist;

console.log("var_hoist: " + typeof var_hoist);	// YES
try {
	let_hoist = true;
	let let_hoist;
	console.log("let_hoist: " + typeof let_hoist);	// NO
} catch (e) {
	console.log("let_hoist: undefined");
}
try {
	const_hoist = true;
	const const_hoist = false;
	console.log("const_hoist: " + typeof const_hoist);	// NO
} catch (e) {
	console.log("const_hoist: undefined");
}

Redeclaration in other scope: var (allowed), let (limited), const (limited)

A var redeclared into a js block is the same as the outside one, while a let or a const are not the same as the outside ones

var var_redecl;
let let_redecl;
const const_redecl = false;
{
	var var_redecl = true;
	let let_redecl = true;
	const const_redecl = true;
}

console.log("var_redecl: " + typeof var_redecl);	// YES
console.log("let_redecl: " + typeof let_redecl);	// NO
console.log("const_redecl: " + const_redecl);		// NO

About constants, they should be initialized on its declaration and cannot be modified after that, but a property of it can change (for example: contant.subvalue or contant.subvalue.subsubvalue or constant[0])

const does not define a constant value, it defines a constant reference to a value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment