Skip to main content

Working with Modern JavaScript Features

A look at some useful JavaScript and TypeScript patterns for cleaner, more maintainable code.

Modern JavaScript has come a long way. Here are some patterns I find myself reaching for regularly.

Destructuring and Defaults

Destructuring with default values makes function arguments much cleaner:

function createUser({ name, email, role = 'user' } = {}) {
  return {
    name,
    email,
    role,
    createdAt: new Date()
  };
}

const user = createUser({ name: 'Alice', email: 'alice@example.com' });
console.log(user.role); // 'user'

TypeScript Utility Types

TypeScript’s utility types are incredibly powerful for type transformations:

interface User {
  id: number;
  name: string;
  email: string;
  createdAt: Date;
}

// Make all properties optional
type PartialUser = Partial<User>;

// Pick only specific properties
type UserPreview = Pick<User, 'id' | 'name'>;

// Omit properties you don't need
type UserWithoutDates = Omit<User, 'createdAt'>;

Async Patterns

The Promise.allSettled method is great when you need to run multiple async operations and handle each result individually:

const results = await Promise.allSettled([
  fetchUser(1),
  fetchUser(2),
  fetchUser(3)
]);

results.forEach((result, index) => {
  if (result.status === 'fulfilled') {
    console.log(`User ${index + 1}:`, result.value);
  } else {
    console.log(`User ${index + 1} failed:`, result.reason);
  }
});

Conclusion

These patterns have become second nature in my daily work. The key is knowing when to reach for them and not over-engineering simple solutions.

What patterns do you find yourself using most often?