---
title: String.prototype.startsWith()
slug: Web/JavaScript/Reference/Global_Objects/String/startsWith
tags:
  - ECMAScript 2015
  - JavaScript
  - Method
  - Prototype
  - Reference
  - String
translation_of: Web/JavaScript/Reference/Global_Objects/String/startsWith
---
<div>{{JSRef}}</div>

<p><span class="seoSummary"><strong><code>startsWith()</code></strong> メソッドは文字列が引数で指定された文字列で始まるかを判定して <code>true</code> か <code>false</code> を返します。</span></p>

<div>{{EmbedInteractiveExample("pages/js/string-startswith.html")}}</div>

<p class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</p>

<h2 id="Syntax" name="Syntax">構文</h2>

<pre class="syntaxbox notranslate"><var>str</var>.startsWith(<var>searchString</var>[, <var>position</var>])</pre>

<h3 id="Parameters" name="Parameters">引数</h3>

<dl>
 <dt><code><var>searchString</var></code></dt>
 <dd>文字列の先頭で検索される文字の集合です。</dd>
 <dt><code><var>position</var></code> {{optional_inline}}</dt>
 <dd><code><var>searchString</var></code> を検索し始めるこの文字列の中の位置です。既定値は <code>0</code> です。</dd>
</dl>

<h3 id="Return_value" name="Return_value">返値</h3>

<p>文字列が指定された文字列で始まる場合は <strong><code>true</code></strong>、それ以外の場合は <strong><code>false</code></strong> です。</p>

<h2 id="Description" name="Description">解説</h2>

<p>文字列が特定の文字列で始まるかどうかを判断できます。(英文字の)大文字・小文字は区別されます。</p>

<h2 id="例">例</h2>

<h3 id="Using_startsWith" name="Using_startsWith">startsWith() の使用</h3>

<pre class="brush: js notranslate">//startswith
let 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
</pre>

<h2 id="Polyfill" name="Polyfill">ポリフィル</h2>

<p>このメソッドは ECMAScript 2015 で追加されました。すべてのJavaScriptの実装でまだ利用可能ではないかもしれません。しかしながら、次のコードで<code>String.prototype.startsWith()</code>をエミュレートできます。</p>

<pre class="brush: js notranslate">if (!String.prototype.startsWith) {
    Object.defineProperty(String.prototype, 'startsWith', {
        value: function(search, rawPos) {
            var pos = rawPos &gt; 0 ? rawPos|0 : 0;
            return this.substring(pos, pos + search.length) === search;
        }
    });
}
</pre>

<p>少々重いですがより強力 (ES2015に完全準拠) な互換実装を <a href="https://github.com/mathiasbynens/String.prototype.startsWith">Mathias Bynens が GitHub</a> で公開しています。</p>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-string.prototype.startswith', 'String.prototype.startsWith')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<p>{{Compat("javascript.builtins.String.startsWith")}}</p>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>{{jsxref("String.prototype.endsWith()")}}</li>
 <li>{{jsxref("String.prototype.includes()")}}</li>
 <li>{{jsxref("String.prototype.indexOf()")}}</li>
 <li>{{jsxref("String.prototype.lastIndexOf()")}}</li>
</ul>