String.prototype.startsWith()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2015.

startsWith() メソッドは文字列が引数で指定された文字列で始まるかを判定して truefalse を返します。

試してみましょう

const str1 = "Saturday night plans";

console.log(str1.startsWith("Sat"));
// Expected output: true

console.log(str1.startsWith("Sat", 3));
// Expected output: false

構文

js
startsWith(searchString)
startsWith(searchString, position)

引数

searchString

str の先頭で検索される文字の集合です。正規表現にすることはできません。正規表現ではない値はすべて文字列に変換されますので、省略したり undefined を渡したりすると、startsWith()"undefined" という文字列を検索します。これはおそらく望むところではないでしょう。

position 省略可

searchString が見つかると期待される開始位置(searchString の先頭の文字のインデックス)です。既定値は 0 です。

返値

文字列が指定された文字列で始まる場合、searchString が空文字列の場合は true、それ以外の場合は false です。

例外

TypeError

searchString正規表現であった場合。

解説

文字列が特定の文字列で終わるかどうかを判断できます。このメソッドでは(英文字の)大文字小文字は区別されます。

startsWith() の使用

js
const str = "To be, or not to be, that is the question.";

console.log(str.startsWith("To be")); // true
console.log(str.startsWith("not to be")); // false
console.log(str.startsWith("not to be", 10)); // true

仕様書

Specification
ECMAScript® 2025 Language Specification
# sec-string.prototype.startswith

ブラウザーの互換性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
startsWith

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

関連情報