Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

JavaScript has always been evolving, introducing new features to make coding more expressive, concise, and powerful. One of the most anticipated features slated for ES2025 is Pattern Matching — a powerful tool that enables developers to destructure and analyze data structures more intuitively and declaratively.

In this blog, we will explore what pattern matching is, how it works in ES2025, and walk through practical examples with detailed explanations.

Simple Literal Matching

const result = match ("hello") {
   case "hello" => "Greeting detected!";
   case "bye" => "Farewell detected!";
   default => "Unknown message"; };
console.log(result);
// Output: Greeting detected!

Explanation: The match expression compares the input string against each case. When it matches the string "hello", it returns the corresponding message.

Destructuring Objects

const user = { name: "John", age: 30 };
const greeting = match (user) {
   case { name, age } if age >= 18 => `Hello, ${name}. You are an adult.`;
   case { name } => `Hi, ${name}. You are a minor.`;
};
console.log(greeting);
// Output: Hello, John. You are an adult.

Explanation: Here, pattern matching destructures the object and applies a guard condition (if age >= 18) to decide which branch to execute.

Matching Arrays with Rest Patterns

const numbers = [1, 2, 3, 4];

const description = match (numbers) {
case [first, second, ...rest] => `First two numbers are ${first} and ${second}, rest are ${rest.join(", ")}`;
};
console.log(description);
// Output: First two numbers are 1 and 2, rest are 3, 4

Explanation: This example matches an array, capturing the first two elements into variables and the rest into another array using the rest pattern. It then constructs a descriptive string.

Nested Patterns

const response = {
status: 200,
data: {
   user: {
     id: 101,
     name: "Alice",
     roles: ["admin", "editor"],
   },
  },
};
const message = match(response) {
     case {
       status: 200,
       data: {
            user: {
            name,
            roles: [firstRole, ...restRoles]
           }
       }
     } =>
     `User ${name} has role ${firstRole} and others: ${restRoles.join(", ")}`;
     case {
       status
     } => `Request failed with status ${status}`;
};
console.log(message);
// Output: User Alice has role admin and others: editor


Explanation:
This example demonstrates matching deeply nested objects. The pattern matches a successful HTTP response and destructures nested properties to extract the user's name and roles.

Matching with Type Guards

function process(value) {
  return match (value) {
    case (x) if typeof x === "string" => `String of length ${x.length}`;
    case (x) if typeof x === "number" => `Number with value ${x}`;
    case (x) if Array.isArray(x) => `Array with ${x.length} elements`;
    default => "Unknown type";
  };
}
console.log(process("hello")); // Output: String of length 5
console.log(process(42)); // Output: Number with value 42
console.log(process([1,2,3])); // Output: Array with 3 elements

Explanation: This example uses guards (if conditions) within pattern matching cases to handle different types dynamically.

Matching Enums or Tagged Unions

const shape = { kind: "circle", radius: 10 };
const area = match (shape) {
     case { kind: "circle", radius } => Math.PI * radius * radius;
  case { kind: "square", size } => size * size;
     case { kind: "rectangle", width, height } => width * height;
  default => 0;
};
console.log(area); // Output: 314.1592653589793sfsdfsfsfs


Explanation:
 This example matches on the kind property of a shape object to compute areas for different shapes, demonstrating how pattern matching can elegantly replace switch-case statements handling tagged unions.

Caveats & Current Status

The Pattern Matching proposal is still evolving as part of TC39 discussions. While the syntax and features showcased here reflect the current proposal drafts, they may change before final inclusion in ES2025. Developers should keep an eye on official updates and consider using transpilers or experimental flags if they want to experiment early.

Conclusion

Pattern Matching in ES2025 promises to be a game-changer for handling complex data structures with clarity and brevity. It combines destructuring, conditional logic, and expressive syntax to help developers write cleaner, more maintainable code.

As JavaScript continues to evolve, embracing new paradigms like pattern matching will empower developers to solve problems more elegantly. Keep an eye out for this feature’s release and try experimenting with it once available!

[References]

Ready to get started?

Contact IVC for a free consultation and discover how we can help your business grow online.

Contact IVC for a Free Consultation
Written by
Author Avatar
Engineering Core
ISB Vietnam's skilled software engineers deliver high-quality applications, leveraging their extensive experience in developing financial tools, business management systems, medical technology, and mobile/web platforms.

COMPANY PROFILE

Please check out our Company Profile.

Download

COMPANY PORTFOLIO

Explore my work!

Download

ASK ISB Vietnam ABOUT DEVELOPMENT

Let's talk about your project!

Contact US