0

Js community I am new in JS and I have a confusion with JS scopes in this example I have an if statement and I defined inside the block var age and this a local scope then I console log this variable age and I got 25 this is why? is it because of the if statement is defined globally so what is defined inside the block is global too? one more thing I noticed the age variable is attached to the global object which is the window I logged it and I found the age var but I am not sure why this is happening?

if(true){
    var age = 25;
}

console.log(age);
  • 1
    For modern JS, try and avoid `var`... Due to functionally scope, what your expecting here is block scope,.. Here you can use `let` & `const`.. – Keith Jun 19 '18 at 23:31
  • 1
    Possible duplicate of [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – faintsignal Jun 19 '18 at 23:42

2 Answers2

1

You have some misunderstandings that should be addressed:

Hoisting

var is hoisted to local scope(before evaluation)

console.log(a) // undefined
var a = 25
console.log(a) // 25

let and const are lexical scoped:

{ // this is a block scope, and will only be a scope when evaluated since it is standalone
    console.log(a) // reference error
    let a = 25;
    console.log(a) // 25
}
console.log(a) // reference error

showing what happens with statement blocks

if (true) {
  let a = 25;
}
console.log(a) // reference error

Control Statements

if statements will only execute if the evaluate to true. true is true. Thus your if statement will always fire in your example and set the hoisted variable to 25.

How you should think of it

console.log(a) // undefined since a got hoisted to top of local scope, which is currently global
var a;
if (false) a = 25;
console.log(a) // undefined
if (true) a = 25;
console.log(a) // 25

More information and what it does to function decleration

Robert Mennell
  • 1,954
  • 11
  • 24
  • var is hoisted to local scope(before evaluation) if the variable is hoisted to local scope why the age variable is attached to global scope and how it works and gives me the value inside the statement block I am not looking for a solution I have a confusion and I want to understand why if I defined a local variable can be accessd globally hope you got it and I appreciate your explination – Ahmed AbdElNasser Jun 19 '18 at 23:59
  • Okay let it be simple, the age var in the if statement block is it local or global? – Ahmed AbdElNasser Jun 20 '18 at 00:10
  • local, which since this is not defined inside of a function is the same as global – Robert Mennell Jun 20 '18 at 00:11
  • Aha I got the point now simply we can say if the statement outside the function the JS consider this as a global block and what you wrote inside it is a global too except let keyword? – Ahmed AbdElNasser Jun 20 '18 at 00:15
  • That depends on what and how its hosting behaviour is defined. Let will clean up after the block like I described earlier – Robert Mennell Jun 20 '18 at 00:17
0

If I understand correctly this is due to a concept called hoisting, variable declarations are moved to the top of the current function scope, not block scope.

https://developer.mozilla.org/en-US/docs/Glossary/Hoisting

wizebin
  • 730
  • 5
  • 15