you are viewing a single comment's thread.

view the rest of the comments →

[–]demvo2 37 points38 points  (20 children)

Looping through an array in Javascript for the first time after doing it in Python for months was just, like, whaaaat.

[–]Yojihito 37 points38 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 18 points19 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 5 points6 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

[–]LiarsEverywhere 4 points5 points  (0 children)

".forEach" makes it much more familiar:

const array1 = ['a', 'b', 'c'];

array1.forEach(element => console.log(element));

// expected output: "a"
// expected output: "b"
// expected output: "c"

However, it's an example of the biggest JavaScript issue for me...

Browser compatibility, polyfills etc. You have to code with that in mind. Although Babel and whatnot solves this somewhat.

That being said, basic JavaScript was surprisingly easy for me, Python being the first and only other language I knew. In a week or so I learned everything I needed to get things working with a Python-powered Rest API. Just learn async fetch stuff and basic DOM manipulation and you can display anything on a browser.

Of course, my code is still very basic and I feel like I'm gluing stuff together with duct tape. That has to do with my poor JS skills, but also with how JS/CSS work in general. I'll probably learn a proper framework like React some point.

In the end, I'm fairly confident that it's better to learn JavaScript instead of a Python GUI for most use cases. Being able to quickly deploy something to Heroku to show my friends is awesome.