From Python to JavaScript

📅April 19, 2021

🕒12 minute read

🏷

python

javascript

This is my story about going from a Python evangelist to a JavaScript crusader and how these two programming languages compare.

I started programming thanks to my unknowingly accurate decision of making Software Engineering my university major. At the moment, I had no idea what I was programming for nor what I wanted to end up doing with these newfound powers.

That quickly turned into me looking to automate anything I could get my hands on. Soon after, I stumbled across Django, Flask and the web. In the course of learning the web, I would have to make changes or create something on the front-end, which involved me breaking most rules and good practices when writing JavaScript.

This ultimately led me to learn the ins and outs of JavaScript. It was admittingly much harder to learn than Python, since it encompassed learning the intricancies of the DOM, HTML, and the way browsers work.

Here are some comparisons I've made based on experience and just general research. Addressing things I feel are important. I assume whoever reading this is familiar with one or both languages.

Disclaimer: this won't be a Python vs JS type thing, nor will I tell you if one is better than the other.

Notable Differences & Similarities

Both Python an JavaScript are dynamically typed, interpreted and multi-paradigm languages that can be applied in many different fields.

I've always loved how easy it is to start writing code and seeing output using both languages. On Python you can simply run an interactive mode session or write .py files and start coding while with JavaScript, it's as easy as opening your browser's devtools or adding a script tag on an HTML file.

Some very notable general-minded differences are:

  • Python has actual classes (classical inheritance model), while JavaScript only supports the Prototypal inheritance model.

  • Formatting in Python is dependant on white-space and indentation while JavaScript hangs on a more traditional syntax with braces.

  • JavaScript runs on the browser (unless you're using Node.js) while Python can be run on a computer or server once installed.

  • Python is multi-threaded while JavaScript runs on its famous single threaded event loop.

Both languages share similar features that I always like to think about when writing code.

  • Both languages support first class functions.

  • Python has a concept of lambda functions while in JavaScript there's arrow functions which can be passed around as expressions between variables and other functions or be used as anonymous functions.

  • Closures can be used in both languages in a similar fashion. For instance, remembering context/state with function invocation, or maybe mocking private members.

  • JavaScript will now have private properties in one of the upcoming ECMAScript releases!

  • Other cool things like async/await and generators.

  • And one of my favorite features, non-mutating array methods like map and filter which can have many uses.

Maps and filters in Python:


numbers = [1,5,10,15,7]

cubed = list(map(lambda num: num * 3, numbers)) # [3,15,30,45,21]

doubleDigits = list(filter(lambda num: num >= 10, numbers)) # [10,15]

Maps and filters in JavaScript:

const numbers = [1, 5, 10, 15, 7];

const cubed = numbers.map((num) => num * 3); // [3,15,30,45,21]

const doubleDigits = numbers.filter((num) => num >= 10); // [10,15]

Python

Everybody loves Python. There's not many reasons to dislike such a simple yet expressive programming language like this one.

As simple as Python seems, some amazing things can be done with it alone, without any dependencies. Python has a grand, rich standard library that can be used for many things. It has a batteries included, plug&play type focus.

My favorite thing about Python is how flexible it is. Even though it's a dynamic, interpreted language, it possess powerful object-oriented functionality, all while being completely portable.

Cool example showcasing Python's expressive simplicity.


from copy import deepcopy
from random import randint

# @return new array with shuffled elements
def shuffle(lst):
    temp_lst = deepcopy(lst)
    m = len(temp_lst)
    while (m):
        m -= 1
        i = randint(0, m)
        temp_lst[m], temp_lst[i] = temp_lst[i], temp_lst[m]
    return temp_lst

Other cool features I love about Python (version 3+) are unpacking, async/await, chained exceptions and multiple inheritance (OOP).

If Python was the language of the browsers, I probably wouldn't have ended up learning and falling for JavaScript.

JavaScript

First of all, I'd like to recommend this very helpful book: JavaScript: The Good Parts. It's great for any newbies out there and a fruitful refresher for any JavaScript experts.

JavaScript has many design flaws and has been critiqued for many years, but can be used to write maintainable code that is easy to debug, given the write toolset.

Along with that, many features are being propsed and added to the ECMAScript standard to follow best practices and possess some more readability.

Here's the same shuffle from the Python example in JavaScript.

// @return new array with shuffled elements
const shuffle = ([...arr]) => {
  let n = arr.length;
  while (n) {
    const i = Math.floor(Math.random() * n--);
    [arr[n], arr[i]] = [arr[i], arr[n]];
  }
  return arr;
};

Some of my favorite JS features include asnyc/await, the Promise API, object and array desctructuring, arrow functions and template literals.

// async await example
async function getGithubUser(username) {
  const response = await fetch(`https://api.github.com/users/${username}`);
  const data = await response.json();
  return data;
}

One thing I dislike greatly about JavaScript is its scarce standard library and the fact that nobody minds. Much of basic functionality that might be needed has to be fetched from npm. That turns into insane dependency trees like these.

One big advantage with JavaScript, is the demand for developer's who can code in it. Companies are moving to the web and that means lots of JavaScript and JavaScript tooling.

Readability

This is a pretty controvertial subject when it comes to programming languages. Python is quite often refferred to as one of the easiest (if not THE easiest) languages to read and I very much agree with that statement. Reading foreign codebases is not much of a hassle when they're written in Python (which is an awesome way to learn best practices).

JavaScript on the other hand... ¯\_(ツ)_/¯. JavaScript can get a bit messy, especially for beginners. Many folks even go as far as calling it ugly, which I disagree with. I believe JavaScript can truly be readable for anyone when following a declarative and functional programming paradigm style.

It always depends on how the code is written and who is the person reading it. I like to think Python can be read by most developers, at least to a certain extent, while it's a bit harder when dealing with JavaScript, but when following best practices, both languages can be explicitely interpreted.

Community

Python and JavaScript are two of the most popular and used languages in our according to Google's stats. The best thing for beginners is that the community is so widespread that you will most likely find help or talks/conferences within most demographics.

Python has gotten very trendy due in part to the rising Artificial Intelligence and Machine Learning communites, which have grown around Python and it's powers. On the other end, JavaScript has gotten popular due to the constant growth of the web and all that encircles it.

Also with roaringly increasing popularity are JavaScript frameworks and libraries, which have been a big controversy for some years. Some modern ones include:

  • Angular

  • React

  • Vue

What I think separates Python from JavaScript communites is the bias and elitism from JavaScriptians. People trying to prove to others why one of these JavaScript libraries is better than the other or why one sucks, when they all serve a just purpose and do so in a very similar manner.

Nevertheless, I feel like both of these languages are headed in the right direction and will be at the top for a while.


Both JavaScript and Python are awesome in their own way and I would encourage learning either one due to their wide landscape of applications. If you're a developer, chances are you'll stumble across one of them in your career.

Python is pretty straightforward to get started with and build something cool (and understand it, of course). If you do that in JavaScript, you'll take a little longer but in the end you'll know why the craze about it exists.