PY
JS

Python to JavaScript

11 lessons

Progress0%
1Variables and Constants2Functions and Closures3Lists vs Arrays4Dicts vs Objects and Map5Classes and Prototypes6Modules and Imports7Async Programming8DOM and Browser APIs9Type Hints vs TypeScript10Error Handling11Build Tools and npm
All Mirror Courses
PY
JS
Functions and Closures
MirrorLesson 2 of 11
Lesson 2

Functions and Closures

Defining functions, closures, and arrow functions

Introduction

In this lesson, you'll learn about functions and closures in JavaScript. Coming from Python, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.

Mirror Card
PY
From Python:

In Python, you're familiar with defining functions, closures, and arrow functions.

JS
In JavaScript:

JavaScript has its own approach to defining functions, closures, and arrow functions, which we'll explore step by step.

The JavaScript Way

Let's see how JavaScript handles this concept. Here's a typical example:

JS
JavaScript Example
// Function declaration
function greet(name, greeting = "Hello") {
  return `${greeting}, ${name}!`;
}

// Arrow function (lambda equivalent)
const square = x => x ** 2;

// Rest params and spread
function log(...args) {
  console.log(args);
}

// Closure
function makeCounter() {
  let count = 0;
  return function increment() {
    count++;
    return count;
  };
}

const counter = makeCounter();
counter(); // 1
counter(); // 2

Comparing to Python

Here's how you might have written similar code in Python:

PY
Python (What you know)
# Function definition
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}!"

# Lambda
square = lambda x: x ** 2

# *args and **kwargs
def log(*args, **kwargs):
    print(args, kwargs)

# Closure
def make_counter():
    count = 0
    def increment():
        nonlocal count
        count += 1
        return count
    return increment

counter = make_counter()
counter()  # 1
counter()  # 2
Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

Arrow functions ≈ lambda (but can span multiple lines)

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

...rest params ≈ *args / no direct **kwargs equivalent

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

Closures work identically — no nonlocal needed in JS

Mirror Card
PY
From Python:

You may be used to different syntax or behavior.

JS
In JavaScript:

Default params look the same in both

Step-by-Step Breakdown

1. Arrow Functions

JavaScript arrow functions are more flexible than Python lambdas — they can contain multiple statements.

PY
Python
square = lambda x: x ** 2
JS
JavaScript
const square = x => x ** 2;
// Multi-line:
const abs = x => {
  if (x < 0) return -x;
  return x;
};

2. Rest Parameters

...rest collects extra arguments into an array, similar to Python's *args.

PY
Python
def fn(*args, **kwargs): ...
JS
JavaScript
function fn(...args) { /* args is Array */ }
Common Pitfall
JavaScript has no **kwargs equivalent. Use a single options object instead: fn(a, { debug: true }).

3. Closures

JavaScript closures capture variables by reference, just like Python. No nonlocal keyword needed.

PY
Python
nonlocal count
count += 1
JS
JavaScript
// Just reference it — JS closures capture by reference
count++;

Common Mistakes

When coming from Python, developers often make these mistakes:

  • Arrow functions ≈ lambda (but can span multiple lines)
  • ...rest params ≈ *args / no direct **kwargs equivalent
  • Closures work identically — no nonlocal needed in JS
Common Pitfall
Don't assume JavaScript works exactly like Python. While the concepts may be similar, the syntax and behavior can differ significantly.

Key Takeaways

  • Arrow functions are multi-line lambdas
  • ...rest ≈ *args, no built-in **kwargs
  • Closures work identically — no nonlocal
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your Python code in JavaScript to practice these concepts.
PreviousNext