Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For a simple phone screen question we generally always ask about scoping rules. It's clearly one of the biggest gotchas of JavaScript. Out of everyone we ask almost everyone insists that in an if or for loop a variable is scoped to that block as opposed to the function.

Is this an unreasonable question?

share|improve this question

closed as primarily opinion-based by durron597, Dan Pichelman, Snowman, MichaelT, GlenH7 Jun 18 '15 at 0:35

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

No it's not. People who don't know that JavaScript does not have block scoping are trivially incompetent and should not be hired. However, you should avoid placing too much emphasis on JavaScript competence for Server-Side web developers – Raynos Mar 30 '12 at 22:03

If its purpose is to test important JavaScript knowledge that you want good candidates to possess, then no. Seems reasonable to me.

share|improve this answer

Actually, as "trivia" (fact that you either know or don't) interview questions go this one has the uncommon virtue that it's something it's hard to imagine an experienced programmer not having encountered, as well as something hard for someone who has encountered it to forget.

share|improve this answer
That would depend on your pre-JavaScript background - if you are used to a language that enforces block level scope you won't think of using variables outside their block and therefore never run into any issues. – Tom Clarkson Mar 31 '12 at 10:20
@TomClarkson - But you would probably eventually try to "redeclare" a variable with "var myVar" inside a block, realize it has kept its old value from outside the block, and look up what's going on. – psr Apr 2 '12 at 16:30
declaring a variable without giving it a value isn't a particularly good idea either. There are patterns where it could come up, such as using a set variable as the exit condition on a search loop, but it's far from universal - maybe you typically use small functions instead of simple blocks or declare all the variables at function level. – Tom Clarkson Apr 2 '12 at 22:01
@TomClarkson - Yeah, I'd buy that explanation in an interview. Though the OP said "insists" it's block level scope. Someone saying "here's why it never came up for me" with a reasonable explanation like yours would be fine. You have good points but I still think it's a relatively good interview "trivia" question. – psr Apr 2 '12 at 22:28

While not necessarily unreasonable, it is a fairly useless question - if you think that the variable belongs to the function, you got the right answer. If you think it belongs to the block you will write good code anyway - how often does a variable declared inside a loop have any meaning outside that loop? Scoping confusion tends to go away if you use appropriate variable names.

share|improve this answer
But if you don't use good names... WHY DID THE VALUE RETURNED CHANGE :) – Justin Thomas Mar 31 '12 at 0:10
In that case you have bigger problems - with no compiler errors available, keeping code easily readable is particularly important. Not sure how you could ask about it as a simple phone question, but it would work quite well as a "what's wrong with this code" question. – Tom Clarkson Mar 31 '12 at 0:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.