DEV Community

Code X Savage
Code X Savage

Posted on

5 JavaScript Features I Wish I Had Learned Earlier

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;
Enter fullscreen mode Exit fullscreen mode

Now it becomes:

const city = user?.address?.city;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Sometimes an empty string is a valid value.

Using ?? fixes that:

const username = "";
const name = username ?? "Guest";

console.log(name); // ""
Enter fullscreen mode Exit fullscreen mode

?? only falls back when the value is null or undefined.


3. Destructuring

Instead of writing:

const name = user.name;
const age = user.age;
Enter fullscreen mode Exit fullscreen mode

You can write:

const { name, age } = user;
Enter fullscreen mode Exit fullscreen mode

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];
Enter fullscreen mode Exit fullscreen mode

You can also merge arrays:

const merged = [...arrayOne, ...arrayTwo];
Enter fullscreen mode Exit fullscreen mode

The spread operator also works with objects:

const user = {
  name: "Alex",
  role: "Developer",
};

const updatedUser = {
  ...user,
  role: "Senior Developer",
};
Enter fullscreen mode Exit fullscreen mode

5. Array Methods

Instead of traditional loops:

for (let i = 0; i < users.length; i++) {
  console.log(users[i]);
}
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

These methods often make your code easier to read.


Bonus Tip: Template Literals

Instead of concatenating strings:

const greeting = "Hello, " + name + "!";
Enter fullscreen mode Exit fullscreen mode

You can use template literals:

const greeting = `Hello, ${name}!`;
Enter fullscreen mode Exit fullscreen mode

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)