DEV Community

Chan Ho Ahn
Chan Ho Ahn

Posted on • Updated on

Similar functions in JavaScript and Python

There are some similarities between computer languages. The two most popular computer languages in these days are probably JavaScript and Python. I have looked in a few useful features between languages. The first three functions I have compared here are basically serving the same purpose with very similar syntax between JavaScript and Python. The purpose of this blog is to show how similar between two languages beyond basic syntax.

First one is Arrow function from JavaScript and Lambda from Python.

Lambda in Python, Arrow function in JavaScript

First, in JavaScript,
We can change this simple function to arrow function in JavaScript. I won't mention about "this" binding with arrow function here but just talk about simplicity of arrow function.

//JavaScript function
 function funcName(param) {
  return param + 10;
}

//JavaScript arrow function
const funcName = (param) => param + 2

Both JavaScript arrow function and Python lambda can be used as a function by assigning function name, or it can just be used as an anonymous function. An anonymous function means the function doesn't have its name assigned which is typically for one time use with serving simple purpose inside another function call. Arrow function in JavaScript serves more than its simplicity such as lexical "this" binding.

#Python function 
 def func_name(param):
      return param + 10

#Python lambda
func_name = lambda param: param + 10

Map function

Map function between JavaScript and Python are almost identical. In JavaScript, forEach function does similar job but it is not used in some cases. map function is used instead where we need to create a new copy of object in a different memory location after map function call. In Python, map function works the same way as not corrupting the original copy in memory but creating a new one to reconstruct new one.

Looking at the following example, "nums" is original copy, and "doubles" is new copy. We don’t corrupt "nums" after running map function.

Map vs. Map
In Python:

 nums = [ 1, 2, 3, 4, 5 ] 
 def double(x):
     return x * 2

 doubles = map(double, nums)

You can use Python lambda here to simplify it. Just one line of code…. Awesome!

 doubles = list(map(lambda x: x * 2, nums))

 #Result:
  [ 2, 4, 6, 8, 10 ]

In JavaScript:
We can also apply arrow function here to simplify it. In JavaScript, Map function is widely used in React.js as for the same reason that "nums" in the following won't be modified after running map function.

  const nums  = [1, 2, 3, 4, 5]
  const doubles = nums.map( x => x * 2);
  console.log(doubles)

  //Result:
  [2, 4, 6, 8, 10]

Filter function

Filer function between JavaScript and Python is similar as what we have seen from map function. The same rule applies here that we don't corrupt the original copy but reconstruct a new one after running filter function.

In Python:
Let's try to take even numbers out from given list.

 nums = [1,2,3,4,5,6]
 evens = list(filter(lambda x : x % 2 == 0, nums))

 #Result:
 [2, 4, 6]

In JavaScript:

 const nums = [1,2,3,4,5,6]
 const evens = nums.filter( x => x % 2 == 0 )
 console.log(evens)

 //Result: 
 [2,4,6]

Conclusion

Learning Python after JavaScript, or learning JavaScript after Python would be a lot of fun. It is like learning another foreign language but the new one you try to learn is already very similar to the one you already can read and write. Both languages are fantastic and its popularity is undeniable in modern computer programming languages.

Top comments (6)

Collapse
 
lyfolos profile image
Muhammed H. Alkan • Edited

I want to be sure but don't think I'm saying you're not good: Do you have any good knowledge with Python or Functional Programming? The wrong parts are

  • Params is shortened version of "Parameters" and you just did get only one parameter.

  • PEP8 - Function names should be lowercase, with words separated by underscores as necessary to improve readability. ("funcName" to "func_name")

  • What are [ comment ]? You just have to use comments! // for JS, # for Python.

  • Why adding space between lambda and map function? (map( lambda x: x * 2, nums))

  • Dude its not ▇▇▇▇▇▇▇ lambda function, its just lambda (anonymous function)!

  • JS and Python lambdas have no any similarities, in my opinion, even Java has more similar lambdas to JS. If you're meaning the feature that lambdas give you, almost every modern haves this feature.

  • Almost every languages map, filter, reduce is similar.

Collapse
 
moz5691 profile image
Chan Ho Ahn

I am very appreciated of your comment. Read some of your post. You seem to have good understanding of functional programming. Following your comment, have made some corrections. Thank you. :)

Collapse
 
lyfolos profile image
Muhammed H. Alkan

No problem!

Collapse
 
ansegundo profile image
Alberto Segundo

In the first python map example, shouldn't it be:

 doubles = map(double, nums)

As in, function double, instead of variable doubles

Collapse
 
moz5691 profile image
Chan Ho Ahn

You're right... I have fixed it. Thank you!!!

Collapse
 
eduardomazolini profile image
Eduardo

doubles = [num * 2 for num in nums]
evens = [num for num in nums if num % 2 == 0]

Is not convert js to python is for by pythonic