JavaScript => Python

Lately I stumbled upon an interesting quote:

“We’re going to learn from Python. JavaScript is pretty close to Python.” (2006)
– Brendan Eich (the creator of JavaScript).

History background

Both Python and JavaScript were born in the nineties (in about 1990 and 1995 respectively). Also both they started as one man project (Python was created by Guido van Rossum, and Javascript by mentioned Brendan Eich).

They represent the object-oriented and interpreted category of programming languages. While Python’s design philosophy emphasizes code readability the JS syntax is intentionally similar to Java. By the way the differences between the JavaScript and Java are more prominent than their similarities – choosing that name was mainly for marketing reasons.

The cool stuff

Functionalities listed below were in Python for years, when comes to JavaScript true evolvement started with ECMAScript2015(ES6) standard, and year by year they keep rolling new release.

1. One-liners

js:

const x = a => a + 10;

py:

x = lambda a : a + 10

2. Iterations over iterable

js:

const numbers = [1, 2, 3, 4, 5];
for (let value of numbers) {
    value += 1
}

py:

numbers = [1, 2, 3, 4, 5]
for value in numbers:
    value += 1

3. Destructing

Rest operator

js:

const [first, ...others] = [1, 2, 3, 4, 5];

py:

first, *others = [1, 2, 3, 4, 5]

Spread operator

js:

const numbers = [1, 2, 3, 4, 5];
const sumOfNumbers = fun(...numbers);

py:

numbers = [1, 2, 3, 4, 5]
sum_of_numbers = fun(*numbers)

Copy

js:

const numbers = [1, 2, 3, 4, 5];
const copiedNumbers = [...numbers];

py:

numbers = [1, 2, 3, 4, 5]
copied_numbers = [*numbers]

4. Destructing Assignment

js:

[a, b] = [b, a];

py:

a, b = b, a

5. String literals

js:

console.log(`Hello, ${name}. You are ${age}`);

py:

print(f’Hello, {name}. You are {age}.’)

6. Importing modules

js:

import package from 'module-name'


py:

from module-name import some_element

7. Set data type

js:

const mySet = new Set([1, 2, 3, 4, 5]);

py:

my_set = set([1, 2, 3, 4, 5])

8. Generator functions

js:

function* generatorLoop(num) {
    for (let i = 0; i < num; i += 1) {
        yield i;
    }
}

py:

def generator_loop(num):
    for i in range(num):
        yield i

9. Array comprehensions

Note: This JavaScript feature is obsolete.
It was initially in the ECMAScript 2015 draft, but got removed.

js:

const numbers = [1, 2, 3, 4, 5];
const doubled = [for (i of numbers) if (i > 1) i * 2];

py:

numbers = [1, 2, 3, 4, 5]
doubled = [i * 2 for i in numbers if i > 1]

Where are we now?

As of 2019 JavaScript and Python are the most commonly used programming languages in the world (https://insights.stackoverflow.com/survey/2019#most-popular-technologies).

While JavaScript is adding new features, Python is jealous for his lack of client-side presence. But it may change too, because some core developers of Python Software Foundation are openly talking about need for new browser and mobile-friendly versions of Python. Both languages are keeping strong and there is not any sort of war between them, they coexist and complement each other. In my opinion, their source of success is providing higher level of abstraction, which I’m big supporter of.


Get Free Email Updates!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

Leave a Reply