JavaScript vs Python For API Programming


JavaScript and Python are two very different programming languages; however, they are both very useful for API programming. I’m choosing to write this blog because of a recent project I did using python, and I felt like I should share my thoughts about both languages. Let’s compare the main differences between these two languages. But first you should know how these languages work!


How Python Works

Python works by taking a .py file, which is sent to the PVM (Python virtual machine) from there it converts the python code to machine readable code. Now the python code is translated into 0’s and 1’s and sent off to the cpu to be processed.

How python works. image from: https://www.geeksforgeeks.org/internal-working-of-python/

How JavaScript Works

JavaScript is a client side language that is served to the user through a web browser, your web browser has a engine for processing JavaScript, from here the engine follows a similar path to python, translating the JavaScript into machine readable code to be processed by the CPU and returned as a working script (hopefully).

image from: https://www.geeksforgeeks.org/introduction-to-javascript-engines/

Application in python

There are many ways to use python with an API, here I have a python CLI weather app. It displays the following information for a local forecast. For this application I used Open-meteo, since they have very good documentation specially for python. I have it set up to either give the user an hourly or daily forecast based off of the input provided. With this approach

py -m weatherapp.py --hourly
py -m weatherapp.py --daily
Weather app using open-meteo API This is the daily model.

One thing I like about API programming in Python is its super simple. Now for something like what I have above there was a lot of code but its back and forth making it tedious but redundant. But python is super modular, which makes it ideal for back end programming.

However one thing I don’t like would have to be taking this and turning it into a GUI, which almost double size of my code or make it bigger depending on which import I use. This also plays into one of the several challenges I faced, that being the imports I used. Formatting a table in a command line is indeed an art, and getting a for loop to input data into each cell was also tedious. So I just used AI to save me from 5 minutes from repetitive typing.


Application in JavaScript

I created something very similar in JavaScript. I started by creating a layout in HTML because I intend to turn it into a web application. I chose to display real time weather information in a table for this application. Here you can see the application.

JavaScript API weather app.

A positive thing about JavaScript is how it seamlessly integrates with HTML making it very easy to develop a GUI for this application. Not to also mention finding answers to an error you encountered is super easy due to how commonly used JavaScript is used across the web. Another plus for JavaScript is documentation and support is everywhere should you need it.

Although a common problem I see with JavaScript is compatibility with browsers. I use Firefox, but I also tested it in edge which is based off of chromium. The error I encountered was very simple and easy to fix (Clearing the cache fixed it).


Using both languages in conjunction

Using both languages together can be very beneficial for load balancing between server side and client side processing. Lets say you have a webapp that needs to process data between two users using virtual machines. These VM’s have very little processing power and the users need to process data. So in your program you can use JavaScript to send information to your python web server using your own API to process this information. By using this approach you can relieve client side programs of important system resources.


Conclusion

While writing this, I reflected on my usage of both programming languages when using an API. In my opinion both languages have their ups and downs. Python is useful for creating server side applications such for processing data submitted by a user. Now JavaScript is very useful for making requests to an API from a client side prospective, which eases server resources and allows the user to process lightweight information.


Similar reads