Python vs JavaScript language features

… JavaScript making you sad makes me sad :cry:

4 Likes

There are quite a few js disapprovers round here, ig don’t take it personally.

1 Like

I take everything personally :neutral_face:

lol I don’t take it personally

2 Likes

Then teach me it better :cry:

I’d be happy to :slight_smile: I even have a tutorial on the basics that has been ‘used’ by around 10k+ people! (it’s also on the learn page)

2 Likes

I completed that one :smiley:
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);
3 Likes

Ngl, never seen one of those except in JavaScript.

1 Like

It exists in plenty of languages including the ‘og’: C…

1 Like

I believe Lua also has it, except it’s called repeat until.

2 Likes

Why is Python bad with loops?

1 Like

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

4 Likes

Wait, so Python is the only “popular” language that uses way less wonky format for for loops (this sounds weird lol)?

2 Likes

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 :wink:

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)
5 Likes

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.

1 Like

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.

3 Likes

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);
1 Like

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

2 Likes

No, that leaks the i variable into the enclosing scope

Oops :grimacing:

1 Like