2

What are the problems that could occur declaring a variable inside an if condition twice?

I understand that this is not the best way of doing it!
I know I can declare the variable outside the if condition.

I'm not looking for a solution! I'm not looking how I can declare my variables. I want to understand why this is a bad approach to declare the a variable inside an if-condition block.

/* Yes I know I can just have condition instead of condition === true, 
   this is only for simplicity */
if(condition === true){ 
    var StuckUps = "over 9000";
}else if(condition === false){
    var StuckUps = "Nothing";
}

alert(StuckUps) /* Yes I can access it outside the if condition!*/

Why would this be a bad habit if only one condition will be executed, so that means the variable will be declare only once anyway. What are the REAL problems with it?

mplungjan
  • 169,008
  • 28
  • 173
  • 236

2 Answers2

7

You are correct that it's not wrong, however as specified in ECMAscript, it will get hoisted to the top of the nearest function definition.

This could lead to problems when having a large codebase and would be "hard" to find.

johniejohnie
  • 523
  • 2
  • 11
  • Correct - and furthermore I would argue that it confuses the heck out of most people when a variable is declared in a deeper scope than it is used. Be nice to your colleagues - write understandable code that's easier to debug! – Mathias Lykkegaard Lorenzen Aug 18 '17 at 08:16
  • That is true so technically there is no programmatic error, but human readable difficulties? –  Aug 18 '17 at 08:18
  • Exactly my point. And strict mode also prevents this behavior as far as I know? – Mathias Lykkegaard Lorenzen Aug 18 '17 at 08:18
  • And, people have learned with experience that declaring the variables at the top of the function is the best way to avoid such issues. See [this](https://stackoverflow.com/a/7357981/5894241). – Nisarg Shah Aug 18 '17 at 08:18
  • Also as Nolyum answered above, if you want the variable being scoped to the if-condition, use the "let" keyword. the "let" keyword will be block scoped, where the "var" keyword is function-scoped. However, the let keyword is available since ES6, so if you're not using ES6 you will not be able to block scope. – johniejohnie Aug 18 '17 at 08:21
0

var goal is to declare global variable, even if you declare it inside another context like function, if/else statement you can access the variable anywhere...

However in most of programming language if you declare a variable inside a block (for, if, function...), the variable only exist inside this block.

In the next generation of javascript let which replace var in most of case. let will have the behavior describe previously.

Another problem is that it can be hard to find where your variable has been declared first time if you get a bug running your script.

Nolyurn
  • 568
  • 4
  • 17
  • Cool thanks I only asked this question because I think its interesting to know what are the real errors, and it seems that most people agree that there arent no programmatic errors but more human difficulties to read it. Would I be correct? –  Aug 18 '17 at 08:22
  • Correct. Javascript is very permisive, in most of other language you can't do that. In javascript, when you use `var`, the variable is stored in the `window ` object as a property. The window object is accessible everywhere, so the variable is accessible everywhere. – Nolyurn Aug 18 '17 at 08:27
  • Thanks I dont know why some people were so rude to me last time I asked this question. I knew this is bad, but its nice to know what are the actual facts and programmatic issues that it could cause. Not to mentioned I got blocked from asking questions now on my real account ¬¬ –  Aug 18 '17 at 08:32
  • Not a programmatic issue, but i don't recommend to do it in order to keep a readable code, easier to debug for you and people helping you or working with you. Have a nice day ;) – Nolyurn Aug 18 '17 at 08:37
  • Yes I dont do it either, was just something i was trying to have people involved to discuss it! –  Aug 18 '17 at 08:38