JavaScript is one of those languages where you can build a lot without knowing every feature. But as I continue building projects, I've come across a few features that make my code cleaner and easier to maintain.
Here are five JavaScript features I wish I had learned much earlier.
1. Optional Chaining (?.)
Accessing nested properties used to look like this:
const city = user && user.address && user.address.city;
Now it becomes:
const city = user?.address?.city;
If any property doesn't exist, JavaScript simply returns undefined instead of throwing an error.
2. Nullish Coalescing (??)
Many developers use ||, but it can produce unexpected results.
const username = "";
const name = username || "Guest";
console.log(name); // Guest
Sometimes an empty string is a valid value.
Using ?? fixes that:
const username = "";
const name = username ?? "Guest";
console.log(name); // ""
?? only falls back when the value is null or undefined.
3. Destructuring
Instead of writing:
const name = user.name;
const age = user.age;
You can write:
const { name, age } = user;
It's cleaner and scales much better.
4. Spread Operator (...)
Creating a copy of an array is incredibly easy.
const numbers = [1, 2, 3];
const copy = [...numbers];
You can also merge arrays:
const merged = [...arrayOne, ...arrayTwo];
The spread operator also works with objects:
const user = {
name: "Alex",
role: "Developer",
};
const updatedUser = {
...user,
role: "Senior Developer",
};
5. Array Methods
Instead of traditional loops:
for (let i = 0; i < users.length; i++) {
console.log(users[i]);
}
Modern JavaScript provides expressive methods:
users.forEach(user => console.log(user));
const activeUsers = users.filter(user => user.active);
const names = users.map(user => user.name);
These methods often make your code easier to read.
Bonus Tip: Template Literals
Instead of concatenating strings:
const greeting = "Hello, " + name + "!";
You can use template literals:
const greeting = `Hello, ${name}!`;
They're cleaner and especially useful for multi-line strings.
Final Thoughts
Learning JavaScript isn't about memorizing every feature.
It's about gradually discovering tools that help you write cleaner, more maintainable code.
The more projects you build, the more these features start to feel like second nature.
Which JavaScript feature do you wish you had learned earlier? Let me know in the comments!
Top comments (0)