Python vs JavaScript language features

You like it better than python because there are multiple keywords to define a variable…?

1 Like

Let and var are not the same thing. From W3School (in case of doubt)

Before ES6 (2015), JavaScript had Global Scope and Function Scope.
ES6 introduced two important new JavaScript keywords: let and const.
These two keywords provide Block Scope in JavaScript.
Variables declared inside a { } block cannot be accessed from outside the block:

4 Likes

Didn’t say better than, just agreed this is a weak point for Python

4 Likes

Comparing two different loops is not an accurate comparison:

let i = 1;
while (i < 10) {
  i += 2;
}

Or

i = 1
while i < 10:
  i += 2

Is a fair comparison, in which case we see very little difference and the only preference for one over the other coming down to preference of syntax (brackets or meaningful whitespaces) (and of course bias).

2 Likes

In JS I would actually prefer the for loop with a let in the for definition to limit the scope and avoid memory leak except when the last value of the i variable is needed. At least in the given example.

3 Likes

Yeah but for the sake of comparison it makes more sense to compare the same thing, Python simply doesn’t really have a statement comparable to the generic for loop

It does actually:

for i in range(1,10,2):
  pass

this increments i of 2

That’s closer to this than a generic forloop:

for (var i in new Array(10)) {

}

You are wrong this js and my python are not the same. Maybe you need to help understand what you mean by generic.

Your original python and equivalent js

i = 1
while i < 10:
  i += 2

Is the same as what I posted above

The main benefit of traditional C for loop syntax is the increment can be anything.
it doesn’t have to be just incrementing or adding more than 1 to the variable. You can do any operation you want as long as it makes some kind of change.
You can even omit creating the variable inside the loop initializer and use an existing variable if desired.
There’s also the issue of scope in py since the variable gets defined outside the loop scope. Can really confuse you if you have a variable already named what you used for the loop and it gets overwritten. Not really that big of a problem but it can turn into an annoyance quickly.

1 Like

This is all true but we are discussing semantic sugar and not capabilities of generic flow control.
Also python has changed and it has added some new semantic sugar that makes this easier if you like it

1 Like

Aside from python constraining you a little bit with range for a little less typing and lacking a traditional for syntax they do the same general things. At least when it doesn’t force you to have to use a while loop where a normal for loop would solve the problem easily.
JS made their iteration sugar wonky though. To actually enumerate the values of an array you can’t use in because in always gives you the index of the value and not the value itself.

for (let index in array) {}

Instead you have to use

for (let value of array) {}

(The highlighting couldn’t even tell it was js without being explicitly told so)

1 Like

Every language has wonky things. :slight_smile: and often the weirdness leven is also influenced by the reader/programmer

1 Like

it would be nice if instead js decided to use like destructure syntax to get both at the same time.
I just figured out you can actually do something similar like this:

for (let [index, value] of ["A", "B", "C"].entries()) {
    console.log(index, value);
}
1 Like

The thing that make js liked (and in my case hated) is that its semantics is enriched by stealing constructs from whatever language with the only goal to make this the one-for-all language.

1 Like

BTW to make things more fun in python is now possible to also do:

i = 0
while (i := i + 1)<10:
  pass

That also works in many other languages including JS:

let i = 0;
while ((i = i + 1) < 10) {
  
}
1 Like

Sure, but in python this is only possible since ( think) 3.8 and most people are not aware of it or fail to understand how it works :slight_smile:

2 Likes

Or:

let i = 0;
while (++i < 10) {

}
4 Likes

NodeJS is considerably faster than Python, at least on Replit.

5 Likes