Learn JavaScript VARIABLE SCOPE in 5 minutes! 🏠

preview_player
Показать описание
// variable scope = where a variable is recognized
// and accessible (local vs global)

let x = 3; // global scope

function1();

function function1(){
let x = 1; // local scope
}

function function2(){
let x = 2; // local scope
}
Рекомендации по теме
Комментарии
Автор

// variable scope = where a variable is recognized
// and accessible (local vs global)

let x = 3; // global scope

function1();

function function1(){
let x = 1; // local scope
console.log(x);
}

function function2(){
let x = 2; // local scope
console.log(x);
}

BroCodez
Автор

This has to be the best explanation for scoping on youtube. I love the examples!

LizyAd
Автор

Best incremental teaching! You are a gifted teacher.

goodness
Автор

This is the best and clearest explanation I can get on youtube!! Your reference of neighbour house makes so much sense to me. Thank you, you’re the best teacher bro🙏

emagenation
Автор

One of the best explanation thanks Bro

learnwithraj
Автор

<*_> This is my seal. I have watched the entire video, understood it, and I can explain it in my own words, thus I have gained knowledge. This is my seal. <_*>

piotrmazgaj
Автор

what about variables declared in other files in global scope ? are they acceible outside that file in global scope ? what is diff between let and var ?

WisdomInExperience
Автор

Hi Bro, couldn't you declare (even if it's bad practice) a local variable to be global inside of a function? For example in python you do: global x = 5. even if it is declared inside of a function it is now global and accessible everywhere. Much love ❤

hunin
Автор

Variables defined with `let` aer scoped to blocks and functions, and can be reassigned
Variables defined with `const` are scoped to blocks and functions, and cannot be reassigned
Variables defined with `var` are scoped to functions or global (if define in block), and it can be reassigned

```
// Block
{
var aVar = 1;
console.log('aVar', aVar);
let aLet = 2;
console.log('aLet', aLet);
const aConst = 3;
console.log('aConst', aConst);
}
console.log('aVar', aVar);
// console.log('aLet', aLet); // Fails
// console.log('aConst', aConst); // Fails

// Function
function myFunc() {
var aFuncVar = 10;
console.log(aFuncVar);
}

myFunc();
// console.log(aFuncVar); // Fails
```

DamianDemasi