Modern JavaScript for Python Developers

Words
442
Reading
2 min
Listen
Play
1y

image.png

JavaScript is unavoidable nowadays, even the most stubborn developer is going to hit it face first if you ever need to collaborate or work cross-discipline.

Even many open source projects ostensibly using a different stack have JS elements (eg. React components on WordPress which is built with LAMP stack).

This quick over view will introduce some key modern JavaScript concepts and how they relate to Python.


let, const, and var

JavaScript uses different keywords to declare variables:

  • let → a changeable variable
  • const → a constant (cannot be reassigned)
  • var → the older way to declare variables (avoid in modern code)

Example:

let score = 5;
score = 10; // allowed

const name = "Jane";
name = "John"; // error: can't reassign

Use const by default, and let if the value will change.


Arrow Functions ()=>

Arrow functions are a shorter way to define functions.

Example:

const greet = (name) => {
  return `Hello, ${name}`;
}

If it’s a single return statement, it can be simplified:

const greet = name => `Hello, ${name}`;

Even though it's written with an arrow function, it's still just a function stored in a variable, so the syntax to call it doesn’t change at all.

const greet = (name) => {
  return `Hello, ${name}`;
};

console.log(greet("hey")); // Output: Hello, hey

Or with the shorter version:

const greet = name => `Hello, ${name}`;

console.log(greet("hey")); // Output: Hello, hey

It works just like a normal function — the arrow function syntax is just a different way to define it, not call it.


Promises and async/await

JavaScript handles asynchronous code using Promises and async/await.

Example:

async function getData() {
  const response = await fetch('https://api.example.com/posts');
  const data = await response.json();
  return data;
}

This pattern helps avoid callback hell and makes async code easier to read.

Without async/await, you might use .then():

fetch('https://api.example.com/posts')
  .then(response => response.json())
  .then(data => {
    console.log(data);
  });

Destructuring

Destructuring is a shorthand way to extract values from objects or arrays.

Example:

const user = {
  name: "Sam",
  age: 30
};

// Without destructuring
const name = user.name;
const age = user.age;

// With destructuring
const { name, age } = user;

This is especially useful in React when working with props.

Destructuring in JavaScript works very similarly to unpacking tuples or dictionaries in Python.

In Python (tuple unpacking):

name, value = get_name_value()

Or with a dictionary:

user = {'name': 'Alice', 'age': 30}
name = user['name']
age = user['age']

Or destructuring with .get():

name = user.get('name')

In JavaScript (object destructuring):

const user = { name: "Alice", age: 30 };

// Without destructuring
const name = user.name;
const age = user.age;

// With destructuring
const { name, age } = user;

Same effect — cleaner syntax, especially when dealing with multiple values.

JavaScript also supports array destructuring (like tuple unpacking):

const [first, second] = [10, 20];
console.log(first); // 10
console.log(second); // 20

Which is just like:

first, second = [10, 20]

If you're familiar with Python unpacking, you're already halfway there. Destructuring in JS is just a more flexible way to pull values out of objects or arrays.


Array Methods: .map(), .filter(), .reduce()

.map() – transform each item in an array

const numbers = [1, 2, 3];
const doubled = numbers.map(n => n * 2);
// [2, 4, 6]

.filter() – only keep certain items

const numbers = [1, 2, 3, 4, 5];
const even = numbers.filter(n => n % 2 === 0);
// [2, 4]

.reduce() – accumulate to a single value

const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
// 10

This is similar to Python’s reduce() function from functools.


Summary

These foundational features in modern JavaScript confuse the eff out of new developers but they are both common and actually useful, not just 'leet' flexing:

  • let and const are safer replacements for var
  • Arrow functions simplify function syntax
  • async/await makes asynchronous code easier to manage
  • Destructuring makes code cleaner and more expressive
  • Array methods like .map(), .filter(), and .reduce() are powerful for transforming data

Once you're comfortable with these, React and modern front-end development will make a lot more sense.

Modern JavaScript for Python Developers | Ecency