Read the proposal to understand what pattern matching will look like in a future version of JavaScript before it ships.
Use the proposal's examples to design your own code today with third-party pattern matching libraries that already exist.
Follow the TC39 design process to understand how a JavaScript language feature moves from idea to standard.
Compare JavaScript's proposed match expression to pattern matching in Rust, Python, or Elixir as a learning exercise.
This is a documentation and proposal repository, there is no code to install or run.
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.
← tc39 on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.