Variable in Modern JavaScript
13 August 2018.

Global /1

var a = 'Sloba Faragovic'; // This is a global variable
function alpha() {
    console.log(a);
}
alpha(); // Outputs 'Sloba Faragovic'

var b = 'Debi Calager';
window.b = 'Freni Calager';
console.log(b); // Outputs 'Freni Calager'

// Come on little to remind

function mod(a,m){
    return a % m;
}
console.log(mod(0,2)); // 0
console.log(mod(1,2)); // 1
console.log(mod(2,2)); // 0
console.log(mod(3,2)); // 1
console.log(mod(4,2)); // 0
console.log(mod(11,3)); // 2

for (let c = 0; c < 5; c++) {
    if (c % 2) {
      console.log(c);
    }
}
console.log(c); // 'ReferenceError: c is not defined'

Global /2

var app = {}; // A global object
app.foo = 'Aja';
app.bar = 'Vanja';
function beta() {
    console.log(app.bar);
}
beta(); // Outputs 'Vanja'

function gamma() {
    c = 'Aja Vanja';
}
gamma();
console.log(c); // Outputs 'Aja Vanja'
console.log(window.c); // Outputs 'Aja Vanja'

Local /1

var a = 'Dana Danic'; // a global variable
function delta(b) {
    // b is a locally
    console.log(b);
}
function epsilon() {
    // c is a locally
    var c = 'Pera Peric';
    console.log(c);
}
delta(a);
epsilon();
console.log(b); // Throws an error as b is undefined in the global scope

Local /2

var d = 'Maca';
function Lipo() {
	if (d === undefined) {
		var d = 'Mica';
	}
	console.log(d);
}
Lipo(); // d nije definisan u funkciji

Local /3

var d = 'Maca';
function Lipo() {
        var d;
	if (d === undefined) {
		var d = 'Mica';
	}
	console.log(d);
}
Lipo(); // d nije definisan u funkciji

Let

var d = 'Maca';
function Lipo() {
	if (d === undefined) {
		let d = 'Mica';
	}
	console.log(d);
}
Lipo();