You can translate JavaScript to Python using Js2Py. It supports whole JavaScript and you can use it to translate large JavaScript modules like esprima.js (a JavaScript 6 parser).


Short demo:

>>> import js2py
>>> f = js2py.eval_js( "function $(a) {return a + arguments[1]}" )
>>> f
function $(a) { [python code] }
>>> f(1, 2, 3)
3

This is how the translated function looks like internally (it's rather ugly):

>>> print js2py.translate_js( "function $(a) {return a + arguments[1]}" )
from js2py.pyjs import *
var = Scope( JS_BUILTINS )
set_global_object(var)
# Code follows:
var.registers([u'$'])
@Js
def PyJsHoistedNonPyName(a, this, arguments, var=var):
    var = Scope({u'a':a, u'this':this, u'arguments':arguments}, var)
    var.registers([u'a'])
    return (var.get(u'a')+var.get(u'arguments').get(u'1'))
PyJsHoistedNonPyName.func_name = u'', PyJsHoistedNonPyName)
Answer from Piotr Dabkowski on Stack Overflow
🌐
freeCodeCamp
forum.freecodecamp.org › t › halfway-through-python-should-i-move-to-js-or-stick-with-python › 55881
Halfway through Python, should I move to JS or stick with Python? - The freeCodeCamp Forum
November 16, 2016 - Hello. Last week I finished my first course. It was a Udacity Nanodegree (introduction to programming) It was very good. I learnt some python, functions and some (mostly theorical) OOP. After that there was a bit of front end teaser, so we learnt a bit of JS, jQuery, the usual.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › python
How to convert Python to JavaScript (and back again) | InfoWorld
March 22, 2023 - JS2Py converts JavaScript to Python, as the name implies, using a pure-Python conversion engine. It has official support only for ES5 right now, although there’s experimental ES6 support for the brave and bold.
Discussions

Conversion from JavaScript to Python code? - Stack Overflow
The definition of a compiler is translates from a higher level language to a lower level language. eg python to machine-code. or java to javascript (google has a rather famous compiler for this somewhere - its' what makes google doc easier to make) Python to javascript compilers abound. More on stackoverflow.com
🌐 stackoverflow.com
Should I switch from JavaScript to python
I’d recommend not learning Python and sticking with HTML/CSS/JavaScript - why? The web is king, if you want to share what you build, you use the web. JavaScript is the only programming language you can use for the frontend. Yes there is more to learn, but it is so rewarding once the pieces start falling together. You can get so much done with just the basics of CSS once you understand Flexbox and/or Grid. HTML is so easy you can learn it in a few hours. What do you want to build? I’d only bother with Python if I was exploring Machine Learning, Statistics, data structures and algorithms, or I wanted to create APIs with Flask/FastAPI. Python is crap for gaming, GUIs, and definitely not first choice for a backend language. Python is a great second language to know, it is a great support language and data language, it’s a great beginner language, but it’s not the best language for almost anything except data. If you just want to build local things for yourself sure it’s a good choice and easy to get started. I say all this as someone who started my programming journey with Python as my first language for about 2 years, before branching out. Really think about what you want to build and choose a language based off that, rather than choosing what’s “easy” More on reddit.com
🌐 r/learnprogramming
44
18
March 19, 2023
Moving from Python to JS

MDN GuideMDN ReferenceThe Rauschmayer books

More on reddit.com
🌐 r/node
15
13
October 4, 2017
I’m switching from Python to JS, what are the best recources online?
If you already understand programming logic learnxinyminutes.com is great for a quick look at the syntax and quirks of a new language. In your case, see https://learnxinyminutes.com/docs/javascript/ For high quality language documentation with examples, I'd recommend MDN. Just type a name of something and add "MDN" after it e.g. array.sort mdn More on reddit.com
🌐 r/learnjavascript
33
26
January 4, 2022
🌐
Medium
medium.com › @zach.talmadge.webdev › code-switch-moving-to-python-from-javascript-with-ease-4871c79e751a
Code Switch: Moving To Python From JavaScript With Ease | by Zach Talmadge | Medium
October 12, 2023 - As mentioned above, Python has lists and dictionaries whereas Javascript has Arrays and Objects. ... Both are used to store collections of items.
Top answer
1 of 4
39

You can translate JavaScript to Python using Js2Py. It supports whole JavaScript and you can use it to translate large JavaScript modules like esprima.js (a JavaScript 6 parser).


Short demo:

>>> import js2py
>>> f = js2py.eval_js( "function $(a) {return a + arguments[1]}" )
>>> f
function $(a) { [python code] }
>>> f(1, 2, 3)
3

This is how the translated function looks like internally (it's rather ugly):

>>> print js2py.translate_js( "function $(a) {return a + arguments[1]}" )
from js2py.pyjs import *
var = Scope( JS_BUILTINS )
set_global_object(var)
# Code follows:
var.registers([u'$'])
@Js
def PyJsHoistedNonPyName(a, this, arguments, var=var):
    var = Scope({u'a':a, u'this':this, u'arguments':arguments}, var)
    var.registers([u'a'])
    return (var.get(u'a')+var.get(u'arguments').get(u'1'))
PyJsHoistedNonPyName.func_name = u'', PyJsHoistedNonPyName)
2 of 4
20

Updated

Now several (4) years later this (almost certainly) can be done; though certainly not with RegEx. I suggest future readers look to @Piotr Dabkowski's answer.. Or some of the other answers. (I don't know having not tried them)


Original Answer

Hm this is a hard one. The definition of a compiler is translates from a higher level language to a lower level language. eg python to machine-code. or java to javascript (google has a rather famous compiler for this somewhere - its' what makes google doc easier to make) Python to javascript compilers abound. technically javascript to python would be a decompiler. (afaik)

I found some speculation about a javascript-python converter here: follow the tread through. it mostly speaks of how it wouldn't be too hard to do. I can't find anything , but that doesn't mean it's no out there.

Regex is not suitable, regex is suitable only for regular languages. programming languages are not normally regular languages. see this

🌐
Reddit
reddit.com › r/learnprogramming › should i switch from javascript to python
r/learnprogramming on Reddit: Should I switch from JavaScript to python
March 19, 2023 -

I stayed away from python at first since I wanted to build cool stuff immediately but as a programming beginner the mingling syntax between js, html and css just frustrates me and I can’t make a lot of progress quickly.

I’m wondering if switching to python would be a good move since I’m studying engineering and I won’t have that frustrating part of not knowing the syntax for three different things.

Just focus on one program and build it correctly you know.

Thanks for reading.

Top answer
1 of 5
15
I’d recommend not learning Python and sticking with HTML/CSS/JavaScript - why? The web is king, if you want to share what you build, you use the web. JavaScript is the only programming language you can use for the frontend. Yes there is more to learn, but it is so rewarding once the pieces start falling together. You can get so much done with just the basics of CSS once you understand Flexbox and/or Grid. HTML is so easy you can learn it in a few hours. What do you want to build? I’d only bother with Python if I was exploring Machine Learning, Statistics, data structures and algorithms, or I wanted to create APIs with Flask/FastAPI. Python is crap for gaming, GUIs, and definitely not first choice for a backend language. Python is a great second language to know, it is a great support language and data language, it’s a great beginner language, but it’s not the best language for almost anything except data. If you just want to build local things for yourself sure it’s a good choice and easy to get started. I say all this as someone who started my programming journey with Python as my first language for about 2 years, before branching out. Really think about what you want to build and choose a language based off that, rather than choosing what’s “easy”
2 of 5
5
Why switch use both? I use JavaScript for web apps and compile them with Vite. For web services I use Python and FastAPI. It is a great combination. Python is also great for talking to hardware. I am using Python on a project to communicate to a NFC reader. Bosses also like it if you can show competency in several languages. I feel like it definitely helped my career.
🌐
ExtendsClass
extendsclass.com › python-to-javascript.html
Python to javascript converter
And it uses Babel to convert ES6 to ES5, it is a JavaScript compiler. JavaScripthon is a simple Python 3.5+ translator to JavaScript. It aims to be able to translate most of the Python's core semantics without providing a full python-in-js environment.
Find elsewhere
🌐
Quora
quora.com › How-do-I-switch-from-Python-to-JavaScript
How to switch from Python to JavaScript - Quora
Answer: You can have them both Pls if ur considering phython or javascript just because your having a hard time on either of them.pls focus on one language first before u learn the other one ,but before you choose js Keep in mind that phyton covers a lot of computer science and web development ...
🌐
DEV Community
dev.to › aveb › from-javascript-to-python-1b1m
From Javascript to Python - DEV Community
December 2, 2019 - Then back to Python3, then CoffeeScript kinda fading away slowly, then tried TypeScript but didnt like it and wont work for me. Then learned Nim which covers JavaScript and WebAssembly now. JavaScript does not really feel like a pure Functional language tho. Good post, keep it up.
🌐
Toolness
hg.toolness.com › python-for-js-programmers › raw-file › tip › PythonForJsProgrammers.html
Python for JavaScript Programmers
The languages actually aren't that dissimilar--in fact, some of JavaScript's latest features have been borrowed directly from Python. Many thanks to those who have commented in my blog post about this tutorial; this rendition of it contains a number of changes as a result of that feedback. This is a good time to explain a bit about Python's design philosophy; hopefully it will give you a better idea of whether this is a language you'd like to use.
🌐
Depalatis
mike.depalatis.net › blog › javascript-for-python-programmers.html
mike.depalatis.net - Javascript for Python programmers
October 10, 2016 - Approaching a little Javascript from the perspective of a Python programmer, you can write good, clear code while avoiding many of the (mostly outdated) common pitfalls often brought up by Javascript detractors. Not that modern Javascript tooling is really so good at reducing complexity…
🌐
GitHub
github.com › majwilson › python-to-javascript
GitHub - majwilson/python-to-javascript: A tool that allows programmers to convert code written in Python into the equivalent code written in Javascript. · GitHub
Specifically we can convert python unit tests which use plain asserts into javascript equivalents which use the mocha test framework and chai assertions. python PythonToJavascriptForTest \ --in_file path/to/python/test_file.py \ --out_dir path/to/javascript/test_folder/
Starred by 27 users
Forked by 5 users
Languages   Python 94.3% | JavaScript 5.7%
🌐
Real Python
realpython.com › python-vs-javascript
Python vs JavaScript for Pythonistas – Real Python
July 31, 2023 - The integration with JavaScript code is difficult and costly. Also, there’s no easy way to call web APIs from it. Note: For a deep dive into WebAssembly, check out The Real Python Podcast - Episode 154 with Brett Cannon. It seems that, after all these years, JavaScript isn’t going away anytime ...
🌐
Medium
medium.com › @JeffyJeff › transitioning-from-javascript-to-python-bridging-the-scripting-divide-7a78f9e76752
Transitioning from JavaScript to Python: Bridging the Scripting Divide | by JF Beaulieu | Medium
December 20, 2023 - JavaScript is also dynamically typed, but it has some differences in type coercion and handling compared to Python. Let’s go more into detail about what type coercion actually is! Type coercion refers to the automatic or implicit conversion of values from one data type to another within an expression.
🌐
STRV
strv.com › blog › how-i-transitioned-from-python-to-node-js-and-why-engineering
Transitioning from Python to Node.js: My Journey
August 30, 2021 - With regards to patterns, Python has more well-defined guidelines compared to Node.js. Node.js seems to lack official guidelines, so the same thing can be made in different ways — which can be annoying and confusing for someone coming from other programming languages. Dealing with errors in JavaScript is more complicated because you can pass your errors through callbacks and then lose control of them, causing a callback hell.
🌐
CodeConvert AI
codeconvert.ai › python-to-javascript-converter
Free Python to JavaScript Converter — AI Code Translation | CodeConvert AI
... Simply paste your Python code into the input box and click the Convert button. Our AI will analyze your Python code and produce equivalent JavaScript code in seconds, preserving the original logic and structure.
🌐
Transcrypt
transcrypt.org
Transcrypt - Python in the browser - Lean, fast, open!
You'll always have immediate access to the newest JavaScript libraries, use the newest DOM functions, interact with the newest HTML or CSS. Calling JavaScript functions from Python, embedding JavaScript code in your Python source, attaching Python event handlers to DOM elements, passing native data between Python and JavaScript, it all happens without any conversion or special syntax.
🌐
Quora
quora.com › How-hard-is-it-to-transit-from-JavaScript-to-Python
How hard is it to transit from JavaScript to Python? - Quora
Answer (1 of 4): I have done the opposite thing—or, I have learned JavaScript in addition to Python after being a Python programmer for half a decade (or something). In general, Python and JavaScript are very similar, and adjusting to the different syntax shouldn’t be much of a problem.