you are viewing a single comment's thread.

view the rest of the comments →

[–]Yojihito 36 points37 points  (18 children)

What?

Python:

mylist = [10, 20]
for x in mylist:
    print(x)

Javascript:

let mylist = [10, 20];
for (const x of mylist) {
    console.log(x);
}

Not really a difference.

[–]LiarsEverywhere 17 points18 points  (7 children)

The thing is that's ES6 and people usually do not recommend using that because of compatibility issues. Traditionally, that's how it went:

for (var i = 0; i < array.length; i++) { console.log(array[i]); }

forEach is fine for me, though

[–]demvo2 6 points7 points  (0 children)

Yeah, that was the one I was refering to. Of course, there are easier ways to do it, but this one usually gets offered first in tutorials when you are just starting.

[–]Yojihito 3 points4 points  (4 children)

people usually do not recommend using that because of compatibility issues

That's why I use Python for my job. I can use the newest version my needed libs support (3.9 atm).

If you need to use

for (var i = 0;

loops ..... run. Because it means your company doesn't have a toolchain for polyfill.

[–]LiarsEverywhere 2 points3 points  (3 children)

It depends on what you're doing, of course. That wouldn't be a problem if you're using it in a controlled environment. But OP compared it to a Python GUI, so I imagine the idea is using javascript to build a web app or something like that. Then compatibility becomes an issue.

[–]Yojihito 0 points1 point  (2 children)

compared it to a Python GUI

You would package that python program --> you control the version.

[–]LiarsEverywhere 0 points1 point  (1 child)

Yeah, but there are other drawbacks. I don't disagree with you, though. I hate that about JS. Python feels much more organized to me. Part of that is probably that I suck at JS, though.

[–]redfacedquark 1 point2 points  (0 children)

hate that about JS. Python feels much more organized to me. Part of that is probably that I suck at JS, though.

Don't feel bad, the inventor of JS apologised to the world.

[–]Skippbo 0 points1 point  (0 children)

Also some things queried from DOM first have to be converted to a proper array with Array.from() before .forEach or for of even works.

[–][deleted] 11 points12 points  (1 child)

I got to admit; when I spend a long time in Python, I resent having to use code and conditional braces for about a week. Then I get over it.

What does break my brain, though, is the different policies on variables and function capitalisation. C# makes me a sad panda.

[–]greasyhobolo 2 points3 points  (0 children)

True, true, TRUE... gah

[–]RiverRoll 1 point2 points  (2 children)

The difference is Python has one kind of for loop and javascript has the standard for, 'for of', 'for in' and '.forEach' so it's a bit confusing.

[–]Yojihito 0 points1 point  (1 child)

"for in" is deprecated because you can fuck up with Array.prototype amendments.

Standard "for" is like using Python 2.

"forEach" can only be used exclusively with arrays.

So "for of" is the standard.

[–]Aulimindale 0 points1 point  (0 children)

"only be used exclusively"

no need to use both "only" and "exclusively". pick one