'Bit confused between JavaScript Hoisting and Functional Scope

If I'm not wrong var lives is functional scope, because it's inside in function sandeep(). But if I'm doing console.log(lives) outside above the function then still I am getting console result as - New Delhi why? Can any one help me. (Is it because of Hoisting? it's moved on top...)

Screen shot without var define inside function enter image description here

Screen shot with var inside function enter image description here

Screen shot- write console.log after function call now it's giving undefined enter image description here

I got my answer - It was my mistake, my chrome browser didn't refreshed properly. Thanks for every one for their answers. enter image description here

console.log("Lives become global variable " + lives); 

function sandeep() {
    lives = "New Delhi";
    return lives;
}
sandeep();


Solution 1:[1]

You are right about the fact that

var lives is functional scope

But you haven't declare the variable in the function. You need to use var lives = "New Delhi" so that its scope is only in the function in which it is declared.

If you directly assign lives = "New Delhi", it is assigned to global window object. Open browser console and try this.

a = 1 and then window.a You'll find that window.a is 1.

Let me know if it helps.

Solution 2:[2]

If I'm not wrong var lives is functional scope

This is right but you forgot the var in front of lives. If you define it as a variable you will get an error:

console.log("Lives become global variable " + lives); 

function sandeep() {
    var lives = "New Delhi";
    return lives;
}
sandeep();

Solution 3:[3]

Only var are working as global & functional scope.

Global scope

var x=5;
function callvar(){
 console.log(x);//5
 var x=3;
 console.log(x);//3
}
colsole.log(x);//3

function scope:

function one(){ var x = 5; 
 console.log(x)//5
}
console.log(x)// reference error

function two(){ var x = 4; 
 console.log(x)//4
}
console.log(x)// reference error

let & const are in block scope

functional scope is one type of block scope

 if(true){
   let x = 5; 
   console.log(x);//5
   }
console.log(x) // reference error

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhinav Jain
Solution 2 caramba
Solution 3 SaimumIslam27