By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,084 Members | 1,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,084 IT Pros & Developers. It's quick & easy.

why jslint suggest we declare local (var) variables at the top of function?

P: n/a
In jslint's doc it says, "JSLint does not expect that a var will be
defined in a block. This is because JavaScript blocks do not have
block scope. This can have unexpected consequences. Define all
variables at the top of the function."

What are some of those unexpected consequences?

Jun 15 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Lee
Jang said:
>
In jslint's doc it says, "JSLint does not expect that a var will be
defined in a block. This is because JavaScript blocks do not have
block scope. This can have unexpected consequences. Define all
variables at the top of the function."

What are some of those unexpected consequences?
People expecting block scope have been surprised to find
that code like the following loops infinitely:

<html>
<body>
<script type="text/javascript">

function demo() {
for(var i=0;i<10;i++) {
document.write(i+":");
for(var i=0;i<5;i++) {
document.write(" "+i);
}
document.write("<br>");
}
}
demo();
</script>
</body>
</html>
--

Jun 15 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.