11月8日、JS Dev Spaceが「JavaScript Just Leveled Up: ES2025」と題した記事を公開した。この記事では、次期ECMAScript仕様「ES2025」で導入される主要な新機能について詳しく紹介されている。

以下に、その内容を紹介する。
パターンマッチング:条件分岐を宣言的に記述
まず注目すべきはパターンマッチングである。
複雑なif-elseやswitch文を、より宣言的で読みやすい構文に置き換えることができる。
従来の条件分岐は以下のように記述していた。
function handleResponse(response) {
if (response.status === 200 && response.data) {
return response.data;
} else if (response.status === 401) {
throw new Error('Unauthorized');
} else if (response.status === 404) {
throw new Error('Not Found');
} else if (response.status >= 500) {
throw new Error('Server Error');
} else {
throw new Error('Unknown Error');
}
}
ES2025では次のように簡潔に書ける。
function handleResponse(response) {
return match (response) {
when ({ status: 200, data }) -> data
when ({ status: 401 }) -> throw new Error('Unauthorized')
when ({ status: 404 }) -> throw new Error('Not Found')
when ({ status: s if s >= 500 }) -> throw new Error('Server Error')
default -> throw new Error('Unknown Error')
};
}
構文はswitch文を拡張したような形で、条件に応じた分岐を自然に記述できる。
また配列パターンにも対応しており、以下のように要素数や構造に基づく分岐も可能だ。
const analyzeArray = (arr) => match (arr) {
when ([]) -> "Empty array"
when ([x]) -> `Single element: ${x}`
when ([x, y]) -> `Two elements: ${x}, ${y}`
when ([first, ...rest]) -> `First: ${first}, Remaining: ${rest.length}`
};
パイプライン演算子:ネスト構造のない関数チェーン
次に「パイプライン演算子(|>)」が追加される。
関数チェーンを直線的に書けるため、ネストが深くなりがちなコードを整理できる。
従来の書き方:
const result = encodeURIComponent(
JSON.stringify(
Object.values(
Object.fromEntries(
Object.entries(data).filter(([k, v]) => v != null)
)
)
)
);
ES2025の書き方:
const result = data
|> Object.entries(%)
|> (%.filter(([k, v]) => v != null))
|> Object.fromEntries(%)
|> Object.values(%)
|> JSON.stringify(%)
|> encodeURIComponent(%);
可読性が大幅に向上し、処理の流れが直感的に理解できる。また、非同期パイプライン(async pipelines)にも対応している。
Record / Tuple:ネイティブなイミュータブル構造体
外部ライブラリに頼らず、ネイティブに不変データ構造(Immutable Data)を扱えるようになる。
// Record — イミュータブルなオブジェクト
const user = #{
id: 1,
name: "Alice",
profile: #{
age: 25,
email: "alice@example.com"
}
};
// Tuple — イミュータブルな配列
const settings = #["dark", "en-US", true];
RecordやTupleは参照ではなく値で比較される。
#{ x: 1, y: 2 } === #{ x: 1, y: 2 } // true!
Reactのコンポーネントでも活用でき、再レンダリングを抑制できる。
const UserCard = React.memo(({ user }) => {
const processed = #{
...user,
displayName: `${user.firstName} ${user.lastName}`
};
return <div>{processed.displayName}</div>;
});
useMemoを多用する必要がなく、シンプルなパフォーマンス最適化が可能となる。
Decimal型:精密な数値演算
長年の課題だった「0.1 + 0.2 !== 0.3」問題がついに解消される。
新たに導入されるDecimal型(接尾辞m)により、誤差のない十進数演算が実現する。
const total = 0.1m + 0.2m; // 0.3m
const tax = 19.99m * 0.08m; // 1.5992m
function calculateOrder(price, qty, taxRate) {
const subtotal = price * qty;
const tax = subtotal * taxRate;
return (subtotal + tax).round(2);
}
金融・課金・科学計算といった精度が重要な分野で特に有用である。
組み込みイテレータAPIの強化
ES2025では、ジェネレータ関数をより扱いやすくする拡張が導入される。
function* fibonacci() {
let [a, b] = [0, 1];
while (true) { yield a; [a, b] = [b, a + b]; }
}
const evenSquares = fibonacci()
.take(20)
.filter(n => n % 2 === 0)
.map(n => n ** 2)
.toArray();
.take(), .filter(), .map(), .toArray()が組み込みでサポートされ、ストリーム処理が遅延評価付きで自然に書けるようになる。
拡張import構文:型指定・条件付きインポート
import文も強化され、JSONやCSS、WebAssemblyなどの外部リソースを型指定して直接読み込める。
import config from './config.json' with { type: 'json' };
import styles from './styles.css' with { type: 'css' };
import wasmModule from './algo.wasm' with { type: 'wasm' };
また、環境に応じた条件付きインポートも可能だ。
const config = await import(
`./config-${process.env.NODE_ENV}.json`,
{ with: { type: 'json' } }
);
try式:関数型エラーハンドリング
従来のtry/catchブロックよりも簡潔に、関数型スタイルで例外を処理できるようになる。
const data = try fetchUserData(userId) catch (err) {
console.warn('Failed to fetch user:', err);
return getDefaultUser();
};
ネストが浅くなり、非同期関数内でも明確なエラーフローが実現する。
Temporal API:日付・時刻の新標準
日時操作のための新API「Temporal」が標準化される。
これにより、LuxonやDay.jsといった外部ライブラリに依存せず、タイムゾーン対応の安全な日付操作が可能になる。
const now = Temporal.Now.instant();
const birthday = Temporal.PlainDate.from('2024-01-15');
const meeting = Temporal.ZonedDateTime.from('2024-12-25T10:30:00[Asia/Shanghai]');
const nextWeek = now.add({ days: 7 });
テンプレートリテラルの改良:安全で整形済みの文字列
HTMLやSQL、CSSなどのテンプレートに特化したリテラル構文も改良された。
const renderHTML = (title, content) => html`
<div class="container">
<h1>${title}</h1>
<div>${content}</div>
</div>
`.dedent();
自動インデント削除、変数の安全な挿入、文脈に応じたエスケープがサポートされる。
開発者への提案
記事では、これらの新機能を活用するためのステップとして以下の実践を勧めている。
- まずはパイプライン演算子やパターンマッチングなどから試すこと。
- Babelプラグインを利用すれば、今すぐ一部機能を体験できる。
- 新しい構文に合わせてチーム内トレーニングを行うこと。
- TypeScriptとの併用で、より安全で堅牢な開発体験を実現できる。
ES2025は単なる構文拡張ではなく、JavaScriptの思考法そのものを進化させるアップデートである。
より安全で、より表現力豊かなコードが、今後の開発者体験を大きく変えていくだろう。
詳細はJavaScript Just Leveled Up: ES2025を参照していただきたい。