Scopes of the var and let Keywords (var scope)

Tell us what’s happening:
unrelated to the challenge but why does the console log on line 8 print block scope if var is defined only within the if statement? they mentioned When you declare a variable with the var keyword, it is declared globally, or locally if declared inside a function.

thank you!

Your code so far

function checkScope() {
"use strict";
  //var i = "function scope";
  if (true) {
    var i = "block scope";
    console.log("Block scope i is: ", i);
  console.log("Function scope i is: ", i);
  return i;

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36.

Variables declared with var always have function scope. It’s let that has block scope. Try uncommenting the first var declaration, and changing the second one from var to let and you’ll see the difference.

thank you very much appreciate it!!

So basically as my fellows mentionod above. var has got ony onyl function scode which means that var u used is accessible only inside function but not outside. and as it s function scoped functions are always on top of every code you write in “execution context” codes that r being executed line by line. And var has got default value of undefined when declared as other variables. but key difference is that the value of var can be give even above from the code we declared
1. = ‘Johongir’
3. console.log(name) // Johongir
4.var name
And if you declare any variable inside function or code block = {} without declaring or defining without keywords var, let or const. they become to global variable. so even if used as this inside {} or function they can be accessed outside of {} and function
so as i said var is given default undefined above all the line > 1 you see in code editer. so that is how we could give new value to it event lines before it is decalred. But with let and consts it is different if they declared inside {} they are only accessible inside {} if tried access outside {} they declared you get error variableUDeclared is not defined. By the way let and const give undefined from the line they are declared so you can change their value as var. Suggestion please have a little bit deeper look to var and never ever use it in your projects it is not good way of writing code insitead use let or const to remove confusion :slight_smile: