Pop Quiz!
Which of these is an infinite loop?
And guess many times console.log will be printed.
A: let 5x3 Loop
for(let i = 0; i &l...
For further actions, you may consider blocking this person and/or reporting abuse
Ha! That's a great illustration for why
let
is better thanvar
:)I actually did have a case where I had to use
var
once - but it was a really obscure hacky-case where I found myself actually wanting to leak the variable scope (because of how a third party library was behaving)... but! I quickly came to my senses and refactored the whole thing so I wouldn't have to do that.I took more time to finish the feature - but I'm sure it was worth it ๐
I still have one use case for
var
- a browser's developer tools.Often I need to quickly test an idea and I write
var foo = 'bar'
then after some thoughts I press
arrow up
on keyboard and change previous input tovar foo = 'baz'
With
let
I getUncaught SyntaxError: Identifier 'foo' has already been declared
and it always frustrating ๐Oh yea, I use
var
in console for the same reason!Fortunately my compiler will report an error If I try something like that.
I think that you should write some hint for beginners, that this kind variable naming is not good pratice and should be avoided.
Oh yea I agree.
At the end, I reminded JD with this:
PSA: Use different names for counters in nested loops - to not only prevent errors, but helps in readability and clarity.
Not too sure if eslint helps with this, but I definitely wish there's an automatic way to catch it.
I made same test with TypeScript and unfortunately this kind of code pass without warning. (:
After that I made same test with another language (Delphi) that Anders Hejlsberg (designer of TypeScript) designed 24 years ago and this old compiler report error. :)
I forgot to mention in my previous comment that your article is great.
This option looks promising: eslint.org/docs/rules/no-shadow
Ahhhh yesss ๐
That 5x3 loop surprised me. But the breakdown of it makes sense.
These sort of challenges always make me laugh as they're really tricky sometimes meanwhile they're examples of what not to do when writing JavaScript. Anti-patterns to an extent. And the principal anti-pattern for js is var. Var is to be considered deprecated for all intense and purposes. Only to be used by pre-compilers and the alike.
And while vat never(or should never) be used in practice, it's still used in coding challenges because it's tricky behavior makes you really question your understanding under the hood of JavaScript.
Ahh, I was wrong! I guessed "C", for the same reason you were initially thinking.
Although I was a bit surprised it would let you declare the variable 2x. I guess because I've been using
let
which would error. egAnyway, the story was fun, too ๐
This is awesome! Man that's a subtle bug, I don't know if I would have spotted it even if it was the cause
Awesome article. How to see more of these!
Thanks!
let a = 3
could be pronounced likelet a equal 3
.Great exercise for teaching the importance of scope to new developers. Great one
Yeah, as a senior dev, I forgot how confusing scoping in Javascript was in the beginning. It was a good exercise for me to find out the common gaps for JS beginners.
I'm afraid this begs for something from the index of the dBASE III Plus manual:
Endless Loop: See Loop, Endless
Loop, Endless: See Endless Loop
Browser shouldn't crash for that...