How to Run JavaScript in Python

JavaScript and Python are two of the most popular and commonly used languages ​​in the world. JavaScript is an integral part of web development, both upstream and downstream. On the other hand, Python is more suitable for back-end coding and rapid application development.

While both have their advantages, you can get the best of both worlds by running JavaScript code from within a Python program.

An introduction to JavaScript and its syntax

JavaScript is a scripting language used for web development. You can use JavaScript to add behavior and functionality to web pages. It is an interpreted language, which means that code runs in real time without a compiler translating it into machine code.

Some of the distinctive syntactic features of the language are:

  1. Code blocks: JavaScript uses braces ({}) to include statements in the same block.
  2. Variable: You can define variables using the var keyword. The syntax to initialize a variable is var variable_name = value;.
  3. Constants: You can define constants using the constant keyword. The syntax to define a constant is, const constant_name = value;.
  4. The operators: You can use various logical operators in JavaScript, including &&, || and !. The equality operator comes in two forms: == compare the value, and === comparing the value and the data type.
  5. Enter exit: You can take input in JavaScript using window.prompt() and display the output on the console using console.log().

If you’re looking to get into full-stack, front-end, or back-end development, you need to understand the basics of JavaScript.

Python Syntax

Python is a high-level programming language that finds application in backend development, artificial intelligence, and data science. Python is considered a dynamically typed and collected interpreted language.

A few key syntaxes in Python to note are:

  1. Code blocks: Python uses indentations to include statements in the same block.
  2. Variable: You can initialize variables in Python like variable_name = value.
  3. Constant: Python doesn’t really support constants, but the convention is that you name variables that shouldn’t change with capitals, e.g. CONSTANT_NAME.
  4. The operators: You can use logical operators like and, Whereand not. Use the equality operator (==) to compare both value and data type.
  5. Enter exit: You can get user input using to input() and display the output using to print().

Python is one of the easiest languages ​​to master and with wide applications you should definitely get your hands dirty with it. These courses and apps are a great place to learn Python for free.

What is the Js2Py module?

Js2Py is a JavaScript to Python translator and JavaScript interpreter written in 100% pure Python by Piotr Dabkowski. To install this module, open your Terminal and run:

pip install js2py

Js2Py automatically translates any valid JavaScript (ECMA Script 5.1, ECMA 6) to Python without using any dependencies. It only uses the standard Python library.

There are three limitations to using the Js2Py module, as stated in this official documentation on GitHub:

  1. Js2py ignores strict mode.
  2. The with statement is not supported.
  3. He processes an indirect call to rating as a direct call to eval.

Despite these limitations, the module works perfectly well and converts most of the JavaScript code into Python language.

Examples of running JavaScript code in Python

Here are some examples of translating and running your JavaScript code in Python:

1. The Hello World program

The first program you write in any language is the Hello World program. You implement it like:

import js2py
js2py.eval_js('console.log("Hello World!")')

The first step is to import the js2py module in your environment. Pass the JavaScript code as a parameter to eval_js() to assess it. Passing console.log(“Hello World!”), it displays Hello World! both on Python’s output terminal and on browser console windows.

2. A function to add two numbers

Here is how you can perform addition of two numbers using JavaScript in Python:

import js2py
js_add = '''function add(a, b){
return a + b;
add = js2py.eval_js(js_add)
print(add(3, 7))

Declare a function in JavaScript format and include it in the multiline string (“”” or ”’). You can store it in a variable called js_add. Pass the function variable to eval_js() to translate it to Python equivalent. Display the result by making a function call and passing two numbers as arguments.

Here is the result obtained for the addition of two numbers using a function in JavaScript:

3. How to convert an entire JavaScript file to a Python file

Suppose you have a JavaScript file named example.js which contains the following code:

function Rectangle(w, h) {
this.w = w;
this.h = h

Rectangle.prototype = {
getArea: function () {
return this.w * this.h

x = new Rectangle(10, 5)

This JavaScript contains a Rectangle function that takes width and height as parameters and returns the area calculated using the width * height formula.

There are two simple methods to convert the JavaScript file to a Python file. They are:

  1. The first method runs the JavaScript file without explicitly translating it. You can implement it like:
    import js2py
    eval_result, example = js2py.run_file('example.js')

    The run_file() takes a JavaScript file as an argument and returns the evaluation result with an object that you can use to run and test any JS code.

  2. The second method explicitly converts and saves the entire JavaScript file to a Python file. You can implement it like:
    import js2py
    js2py.translate_file('example.js', '')

    Using the first method, you can work with the JavaScript file in your Python environment itself. However, if you just want to convert and run the file in Python, the second method is more feasible.

4. Creating an object using the new function

One thing to note is that you cannot create an object from the JavaScript file like new example.Rectangle(8,2). Instead, you should use .new(8,2). You can code it as follows:

import js2py
eval_result, example = js2py.run_file('example.js')
rect =,2)

Using the first method, translate the JavaScript file that contains the code to calculate the area of ​​a rectangle. Use the point (.) the operator to access the Rectangle function in the evaluated file and use the New() function to create its object. Pass two numbers as argument. Use the getArea() function to calculate the area and display it using the to print statement.

Here is the output for calculating the area of ​​a rectangle in JavaScript:

Run Python in the browser

Although Python is a popular tool you can use to code on the backend, there are a few tools you can explore to try it out on the frontend as well. You can use Brython, Transcrypt, and Pyjs to write JavaScript-based web applications entirely in Python.

You can use other strategies to combine Python and JavaScript code in the same application. The simplest is to communicate via a simple and open standard like JSON.

Comments are closed.