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 variableconst
→ 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
andconst
are safer replacements forvar
- 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.
This is a great post, I’m a JavaScript developer by day but seldom use Python. Great to see posts like this on hive!
I used to teach JavaScript when it first came out in the 90s, but I do so much backend Python and PHP that I rarely have to do serious work with it nowadays, not since the jQuery days, so more modern idioms still make my brain itch :)
We use Vue at our company rather than react as I suggested it when I joined and the CTO agreed. Is served us very well and nowadays we use TypeScript along with GraphQL.
I've done very little JavaScript, but then I don't work with web stuff. I'm happy doing Python, but it's always good to have a range of skills. I just find that I forget stuff if I don't use it much.
Yeah same, I used to use .net every day but I’m at least a decade out of date
Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!
Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).
You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.