Posted on • Originally published atdjangotricks.com on
Equivalents in Python and JavaScript. Part 3
This is the 3rd part of 4-article series about analogies inPython andJavaScript. In theprevious parts we covered a large part of the traditionalPython 2.7 andJavaScript based on the ECMAScript 5 standard. This time we will start looking intoPython 3.6 andJavaScript based on the ECMAScript 6 standard. ECMAScript 6 standard is pretty new and supported only the newest versions of browsers. For older browsers you will needBabel to compile your next-generationJavaScript code to the cross-browser-compatible equivalents. It opens the door to so many interesting things to explore. We will start from string interpolation, unpacking lists, lambda functions, iterations without indexes, generators, and sets!
Variables in strings
The old and inefficient way to build strings with values from variables is this concatenation:
name='World'value='Hello,'+name+'!\nWelcome!'
This can get very sparse and difficult to read. Also it is very easy to miss whitespaces in the sentence around variables.
SincePython version 3.6 andJavaScript based on the ECMAScript 6 standard, you can use so called string interpolation. These are string templates which are filled in with values from variables.
InPython they are also called f-string, because their notation starts with letter "f":
name='World'value=f"""Hello,{name}!Welcome!"""price=14.9value=f'Price:{price:.2f} €'# 'Price: 14.90 €'
InJavaScript string templates start and end with backticks:
name='World';value=`Hello,${name}!Welcome!`;price=14.9;value=`Price${price.toFixed(2)} €`;// 'Price: 14.90 €'
Note that string templates can be of a single line as well as of multiple lines. For f-strings inPython you can pass the format for variables, but you can't call methods of a variable unless they are properties and call getter methods.
Unpacking lists
Python and nowJavaScript has an interesting feature to assign items of sequences into separate variables. For example, we can read the three values of a list into variables a, b, and c with the following syntax:
[a,b,c]=[1,2,3]
For tuples the parenthesis can be omitted. The following is a very popular way to swap values of two variables inPython:
a=1b=2a,b=b,a# swap values
With the next generationJavaScript this can also be achieved:
a=1;b=2;[a,b]=[b,a];// swap values
InPython 3.6 if we have an unknown number of items in a list or tuple, we can assign them to a tuple of several values while also unpacking the rest to a list:
first,second,*the_rest=[1,2,3,4]# first == 1# second == 2# the_rest == [3, 4]
This can also be done withJavaScript (ECMAScript 6):
[first,second,...the_rest]=[1,2,3,4];// first === 1// last === 2// the_rest === [3, 4]
Lambda functions
Python andJavaScript have a very neat functionality to create functions in a single line. These functions are called lambdas. Lambdas are very simple functions that take one or more arguments and return some calculated value. Usually lambdas are used when you need to pass a function to another function as a callback or as a function to manipulate every separate elements in a sequence.
InPython, you would define a lambda using thelambda
keyword, like this:
sum=lambdax,y:x+ysquare=lambdax:x**2
InJavaScript lambdas use the=>
notation. If there are more than one arguments, they have to be in parenthesis:
sum=(x,y)=>x+y;square=x=>Math.pow(x,2);
Iteration without indexes
Many programming languages allow iterating through a sequence only by using indexes and incrementing their values. Then to get an item at some position, you would read it from an array, for example:
for(i=0;i<items.length;i++){console.log(items[i]);}
This is not a nice syntax and is very technical - it doesn't read naturally. What we really want is just to grab each value from the list. AndPython has a very neat possibility just to iterate through the elements:
foritemin['A','B','C']:print(item)
In the modernJavaScript this is also possible with thefor..of
operator:
for(letitemof['A','B','C']){console.log(item);}
You can also iterate through a string characters inPython:
forcharacterin'ABC':print(character)
And in the modernJavaScript:
for(letcharacterof'ABC'){console.log(character);}
Generators
Python and modernJavaScript has a possibility to define special functions through which you can iterate. With each iteration they return the next generated value in a sequence. These functions are called generators. With generators you can get numbers in a range, lines from a file, data from different paginated API calls,fibonacci numbers, and any other dynamicly generated sequences.
Technically generators are like normal functions, but instead of returning a value, they yield a value. This value will be returned for one iteration. And this generation happens as long as the end of the function is reached.
To illustrate that, the followingPython code will create a generatorcountdown()
which will return numbers from the given one back to 1, (like 10, 9, 8, ..., 1):
defcountdown(counter):whilecounter>0:yieldcountercounter-=1forcounterincountdown(10):print(counter)
Exactly the same can be achieved in modernJavaScript, but notice the asterisk at thefunction
statement. It defines that it's a generator:
function*countdown(counter){while(counter>0){yieldcounter;counter--;}}for(letcounterofcountdown(10)){console.log(counter);}
Sets
We already had a look at lists, tuples and arrays. But here is another type of data - sets. Sets are groups of elements that ensure that each element there exists only once. Set theory also specifies set operations likeunion, intersection, and difference, but we won't cover them here today.
This is how to create a set, add elements to it, check if a value exists, check the total amount of elements in a set, and iterate through its values, and remove a value usingPython:
s=set(['A'])s.add('B');s.add('C')'A'inslen(s)==3forelemins:print(elem)s.remove('C')
Here is how to achieve the same in modernJavaScript:
s=newSet(['A']);s.add('B').add('C');s.has('A')===true;s.size===3;for(letelemofs.values()){console.log(elem);}s.delete('C')
The Takeaways
- String interpolation or literal templates allows you to have much cleaner and nicer code even with a possibility to have multiple lines of text.
- You can iterate through elements in a sequence or group without using indexes.
- Use generators when you have a sequence of nearly unlimited elements.
- Use sets when you want to ensure fast check if data exists in a collection.
- Use lambdas when you need short and clear single-line functions.
As you know from the previous parts, I am offering a cheat sheet with the whole list of equivalents inPython andJavaScript, both, time honored and future proof. To have something printed in front of your eyes is much more convenient than switching among windows or scrolling up and down until you find what you exactly were searching for. So I suggest you to get the cheat sheet and use it for good!
Get the Ultimate Cheat Sheet of
Equivalents in Python and JavaScript
✨✨✨
In thenext and last part of the series, we will have a look at function arguments, classes, inheritance, and properties. Stay tuned!
Cover photo byAlex Knight
Top comments(5)

- LocationBerlin, Germany
- EducationVilnius University, Lithuania
- WorkFounder at "1st things 1st"
- Joined

You’re creating a false equivalency between arrow functions and lambda functions. Just because they look syntactically similar doesn’t mean that they have the same behavior.
Lambda functions are Python’s anonymous functions.
Arrow functions are not JavaScript’s anonymous functions, because any type of function can be anonymous.
Both lambda functions and JS functions can be bound to a reference (although you should never do this in Python)
I think the key point here is that functions are first order objects in JavaScript and Python. That’s how they’re similar.

- LocationBerlin, Germany
- EducationVilnius University, Lithuania
- WorkFounder at "1st things 1st"
- Joined
I would say that they might be implemented differently architecturally, but the behaviors of my given examples are equivalent.

- Educationself-taught
- Joined
Really enjoyed this article. Shared it with my team.

- LocationBerlin, Germany
- EducationVilnius University, Lithuania
- WorkFounder at "1st things 1st"
- Joined
Thanks. I am glad you liked it :) The last part will also be interesting.
For further actions, you may consider blocking this person and/orreporting abuse