explaingit

tc39/proposal-pattern-matching

5,781HTMLAudience · developerComplexity · 1/5Setup · easy

TLDR

A formal proposal to add pattern matching to JavaScript, a cleaner way to check the shape of a value and act on it, similar to switch but far more expressive and usable as an expression.

Mindmap

mindmap
  root((Pattern Matching))
    What it does
      JS language proposal
      Shape based branching
      Expression not statement
    Pattern Types
      Value patterns
      Structure patterns
      Combinator and or
    Compared to Today
      Replaces switch limits
      Shorter than if else
      Nestable in expressions
    TC39 Process
      Stage 1 proposal
      Active design phase
      Committee review
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Read the proposal to understand what pattern matching will look like in a future version of JavaScript before it ships.

USE CASE 2

Use the proposal's examples to design your own code today with third-party pattern matching libraries that already exist.

USE CASE 3

Follow the TC39 design process to understand how a JavaScript language feature moves from idea to standard.

USE CASE 4

Compare JavaScript's proposed match expression to pattern matching in Rust, Python, or Elixir as a learning exercise.

Tech stack

HTMLJavaScript

Getting it running

Difficulty · easy Time to first run · 5min

This is a documentation and proposal repository, there is no code to install or run.

In plain English

This repository contains a formal proposal to add pattern matching to JavaScript, submitted to TC39, the committee that decides what gets added to the language. The proposal is at Stage 1, meaning the committee has agreed the problem is worth solving but the solution is still in active design. The problem the proposal addresses is checking the shape of a value and acting differently based on what you find. JavaScript's existing tools for this are awkward. The switch statement forces you to use strict equality checks, requires explicit break statements to avoid bugs, and cannot be used as an expression. The if/else chain is flexible but verbose: you have to repeat the path into a data structure multiple times for each check you want to perform. The proposal introduces a match expression, written as match, followed by a block of clauses. Each clause tests the input against a pattern, and the whole expression resolves to the value of whichever clause matches first. Because it is an expression, you can assign it to a variable, return it from a function, or use it inside another expression, none of which is possible with switch. The patterns themselves form a small sub-language. Value patterns check whether a property holds a specific value. Structure patterns check whether an object or array has a certain shape and let you test nested parts in the same step. Combinator patterns combine multiple checks with logical and/or rules. The proposal also adds a standalone is operator for quick one-off tests. Similar features exist in Rust, Python, F#, Scala, and Elixir, and the proposal draws from all of them. Several third-party JavaScript libraries already provide similar functionality in user space, listed in the README, while the official language feature goes through the committee review process. The full README is longer than what was shown.

Copy-paste prompts

Prompt 1
Show me how the proposed JavaScript match expression would simplify this switch statement I have that checks the type and shape of API response objects.
Prompt 2
Using a third-party JavaScript pattern matching library listed in the tc39/proposal-pattern-matching repo, rewrite my nested if-else block that checks user role and subscription status.
Prompt 3
Explain what a combinator pattern is in the TC39 pattern matching proposal and give me an example of using and or conditions together.
Prompt 4
How does the proposed is operator in tc39/proposal-pattern-matching work, and when would I use it instead of a full match expression?
Open on GitHub → Explain another repo

← tc39 on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.