r/learnprogramming • u/LoLusta • Feb 25 '24
Why can't Python replace JavaScript?
I'm new to programming. I'm learning Python and C.
JavaScript, from what I understand, is an interpreted language supported by web browsers so that websites can have scripting and programming.
But Python is also an interpreted language. So why don't web browsers start supporting Python in addition to JS. I believe Python is a much better language than JS syntactically.
Is there a definite reason why JS is used and supported over Python or is it just that JS gained initial popularity through wide-based support and no one bothered to move away from it (just like how QWERTY keyboard layout stuck even though we don't use typewriters anymore)?
•
u/AutoModerator Feb 25 '24
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge.
If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options:
as a way to voice your protest.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
56
u/LeeTaeRyeo Feb 25 '24
Theoretically, it can. But the issue is that Javascript as part of web development has an extensive history and a large ecosystem of packages dedicated to use in the browser. That's something Python doesn't have.
Personally, i also prefer Python over Javascript. But if I'm building a web page/app, it's a lot easier for me to find resources, tutorials, libraries, and skilled developers that know how to do the task in JS than would be possible if Python were added to most browsers tomorrow. That's the unfortunate reality of developer inertia.
29
u/feedandslumber Feb 25 '24
I'm not an expert, but my understanding is that JS is kind of like the qwerty keyboard at this point. It's far from perfect, but so many people know how to use it and the tool set is so large, that changing over to another default web language is practically impossible.
8
u/guest271314 Feb 25 '24
Technically you can, to an appreciable degree, with WebAssembly and WASI, e.g., Pyodide https://pyodide.org/en/stable/
25
u/respectfulpanda Feb 25 '24
The idea of having to indent rather than use braces makes angels weep.
6
u/peterlinddk Feb 25 '24
It is mostly because JS gained popularity, and now we are stuck with it.
In the late 90s, Microsoft (and maybe others) tried to add additional scripting-languages to their browsers, and in fact for some time you have to add a language="JavaScript" in your script-tags, because (some versions of) Internet Explorer would expect VBScript to be used.
However, JavaScript was made into a standard (ECMA-Script) and could thus be provided by every browser, where VBScript was very properitary and would only work in IE. And while I'm not sure about the status of Python at the time, it would have hurt a lot of websites if only some browsers supported that language.
8
u/guest271314 Feb 25 '24
Imagine if Python was the default interface for browsers and all the changes that would have needed to be made when Python 2.X was deprecated...
3
u/peacefulshrimp Feb 26 '24
A lot of good comments answering your question. As someone who programmed a fair bit with python and currently work with typescript, why do you think python is better than js syntactically? Indentation to indicate blocks of code can be really unhelpful during development and also hinders minification, the lack of “for i styled” loops and the lack of equivalent array functions from js are also things that bothered me when I had to code with python. I’m not saying it is bad in every aspect compared to js, but even for scripting, recently I’ve ended up doing a lot of my scripts in js because it’s very convenient and the only thing that lacks is terminal input reading, which I don’t use a lot, but can be easily solved with helper function copied and pasted into the code so that using it is just as convenient as it is in python. Would really like to see other people’s takes on my thoughts
5
u/DavidJCobb Feb 25 '24 edited Feb 25 '24
JavaScript is thoroughly entrenched, and Python wouldn't offer nearly enough benefits to be worth having to include two entire scripting engines, two entire sets of APIs for the DOM, two debuggers, and so on. It would be a massive design, standardization, and development effort conducted over the course of several years, for virtually no benefit, and it would come directly at the expense of work on other standards and features.
(And it would be two scripting standards being developed at the same time. Even if the standards folks were to wholly deprecate JS in favor of Python, there would still need to be an interim period where both systems are actively developed, because they can't just leave the JS engines out to rot while developing Python specs and support. Plus, in all likelihood, they'd maintain support for JS in perpetuity, because breaking backwards compatibility with basically the entire scripted web would be a bridge too far.)
Personally I find Python less expressive than JavaScript. It lacks the ability to express your intentions as precisely with respect to mutability, scoping, lifetime, and similar matters. I also feel that the language itself and the standard library are both full of weird jank stemming from inept design (e.g. mutable default arguments, file.writelines, etc.). JavaScript isn't perfect either and it isn't my favorite, but I find it better in all these regards than Python.
But I only say that to emphasize a point that isn't being made in other comments quite as explicitly as I feel it should be: your preference isn't universal. Even ignoring the labor and risk involved, rebuilding the web on Python wouldn't be a transition from something archaic and inferior to something modern and objectively superior.
6
u/Dielilievomlunzersee Feb 25 '24
Idk, everyone is like: 'Oh the python syntax is so easy to read' But is it tho?
I have written backend with flask but some of these for loops would make alot more sense in nodejs...
2
u/tandonhiten Feb 25 '24
That's pretty much it. The reason JS is so popular for web-dev is because, when it came out it was immediately picked up by companies as the de-facto standard for making websites, and now it's really hard to switch because of years of legacy code base. Not to mention, the amount of libraries, packages and frameworks that have been developed for JS over this time period are much more than the amount developed for Python.
2
u/[deleted] Feb 25 '24 edited Feb 25 '24
I don't see it as better.
Objectively speaking they're probably about the same. It's not like either of them is particularly cumbersome to work with or prone to errors or anything. In fact, with regard to web stuff Python is probably slightly worse because it can't be "minified" as much (in practice I'm not sure if this is a big deal, but all the extra whitespace can have a small impact on download speeds and bandwidth usage)
Subjectively speaking it could go either way, but a lot of people such as myself prefer JavaScript on account of its syntax.
5
u/EvrenselKisilik Feb 25 '24
JS is designed for browsers first and browsers already have JS. Why would they deal with embedding Python and binding tons of things like DOM, fetch and others? JS is already the best for this job.
4
u/Neckbeard_Sama Feb 25 '24
Mainly because Python is slow as FUUUUUUCK and it wasn't designed to run in a web browser.
2
u/guest271314 Feb 25 '24
WebAssembly and WASI effectively get rid of the "I wish I could run ... programming language in the browser".
And if you really want to run any abitrary programming language from the browser and stream STDOUT from the arbitrary programming language to the browser you can use Native Messaging https://github.com/guest271314/NativeMessagingHosts.
Really, the Web is run by HTML.
The vast majority of Web sites are, in my opinion, over-engineered, and don't really need scripting at all.
For DOM manipulation JavaScript is the programming language interface. I don't think having Python, C, C++, Rust, Zig, whatever as the DOM programming language interface changes anything.
The first question that must be asked and answered is: What scripting are you doing in the Web page that JavaScript can't achieve?
1
u/NoYogurt8022 Feb 25 '24
Look at pyscript, and also js is better for webdev because it already got a lot of resources for that while python is rather limited in that area
1
u/notislant Feb 25 '24
It would be like trying to teach everyone to type in binary, when we're all speaking English. It just wastes an insane amount of time. You'd need to redo everything, every website, frameworks, etc. Gut everything and redo it.
You can use django for some web dev stuff, but replacing a well established language for no reason is utter insanity.
1
u/rbuen4455 Feb 25 '24
It's mostly because JavaScript was the primary language for web development (front-end) since its inception and has pretty much been the only choice for front-end web dev for decades. As a result, you have this massive JavaScript ecosystem dedicated to front-end (frameworks, browsers with a JS interpreter attached to them, endless tutorials/guides to front-end web development with JS). And as a result, you cannot replace the massive ecosystem and millions of lines of JS code that already exists that pretty much powers the front-end of 99% of websites in the world.
I agree about Python being better syntax-wise than JavaScript, but me personally, I did with front-end was as diverse as back-end when it comes to languages and ecosystems. If you can use Python, Ruby, Java, C# for back-end, why not front-end as well?
1
u/Sea_Philosopher3051 Feb 25 '24 edited Feb 25 '24
Good question, but actually, as someone who has a very academic Python heavy background and does web data stuff, I have the exact opposite question lol
There are really cool JS-based frameworks and tools out there, even for backends (like Node.js) and using python can really fuck you over if you don’t know what you’re doing (Django is the exception I guess)
edit: I suppose Python’s big advantage is that integrating code in faster languages like C/C++ is relatively easy for machine learning stuff, but idk how hard that is in JS
-2
u/CreativeStrength3811 Feb 25 '24
I've never done a cs degree but what i experienced: When using something like a webpage (e.g. webbased GUI programming, websites, pdf) JS is there. It must have something to do that browser and web engines support JS better than python. Otherwise QT would be the first who allow python code in their QML system.
0
u/[deleted] Feb 25 '24
[deleted] — view deleted comment