… JavaScript making you sad makes me sad
There are quite a few js disapprovers round here, ig don’t take it personally.
I take everything personally
lol I don’t take it personally
Then teach me it better
I’d be happy to I even have a tutorial on the basics that has been ‘used’ by around 10k+ people! (it’s also on the learn page)
I completed that one
Anyways I already have basic understanding of nodejs, and more basic of web js. I don’t understand stuff like loops yet (why are they so complicated???) xD
Loops are pretty simple, you’ve got the same three generic loops as most languages (Python is bad with loops), the for
loop, the while
loop and the do while
loop.
for (variable; condition; increment) {
// ...
}
while (condition) {
// ...
}
do {
// ...
} while (condition);
Ngl, never seen one of those except in JavaScript.
It exists in plenty of languages including the ‘og’: C…
I believe Lua also has it, except it’s called repeat until
.
Why is Python bad with loops?
It is not, it is just js users want to keep their ideals that the wonky (variable; condition; increment)
format is “generic” and “good practice” XD
Wait, so Python is the only “popular” language that uses way less wonky format for for loops (this sounds weird lol)?
Python doesn’t really properly implement loops, especially the most commonly used one, the for
loop.
So do C, C++, C#, Java, and PHP (and probably more) users
In my opinion, this syntax is more robust and covers more use cases.
At first glance, without knowing the language syntax, which one of these makes more sense:
// You can clearly see how i is initalized and incremented
for (let i = 1; i < 10; i += 2) console.log(i)
or
# What do 1, 10, and 2 mean???
for i in range(1, 10, 2): print(i)
As @GrimSteel said, it’s a standard used across a plethora of languages, nothing to do with JS. Python ‘for
’ loops are really only an iterable loop like for in
or for of
or other equivalents in other languages.
You’re right, the JavaScript example looks better and makes more sense.
However, I would say that 70% of loops in Python don’t use range()
, instead they loop through an iterable (list, set, dictionary, tuple). When looping through an iterable
example = ["a", "b", "c"]
for letter in example: print(letter)
# This makes more sense, doesn't it?
It’s much easier to tell what’s happening.
JavaScript has that too: (and, like @MattDESTROYER said, many other languages also have an equivalent - forEach
is pretty common AFAIK)
const example = ["a", "b", "c"]
for (const letter of example) console.log(letter);
Doesn’t that make much more sense? Unfortunately, many people don’t use that in JS.
And wouldn’t a while loop be easier for your example?
var i = 1;
while(i < 10) {
console.log(i)
i += 2
}
// Doesn't this make sense, too?
P.S. It seems people have gotten too off-topic
No, that leaks the i
variable into the enclosing scope
Oops