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

J

Jang

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?
 
L

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>


--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top