String.prototype.startsWith()

  • Revision slug: JavaScript/Reference/Global_Objects/String/startsWith
  • Revision title: String.startsWith
  • Revision id: 335613
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{js_minversion_header("1.8.6")}}

Determines whether a string begins with the characters of another string, returning true or false as appropriate.

Method of String
Implemented in JavaScript 1.8.6
ECMAScript Edition None (Harmony Proposal)

Syntax

var startsWith = str.startsWith(searchString [, position]);

Parameters

searchString
The characters to be searched for at the start of this string.
position
The position in this string at which to begin searching for searchString; defaults to 0.

Description

This method lets you determine whether or not a string begins with another string.

Examples

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

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

See also

Revision Source

<div>
  {{js_minversion_header("1.8.6")}}</div>
<p>Determines whether a string begins with the characters of another string, returning <code>true</code> or <code>false</code> as appropriate.</p>
<table class="standard-table">
  <thead>
    <tr>
      <th class="header" colspan="2" scope="row">Method of <a href="/en-US/docs/JavaScript/Reference/Global_Objects/Function" title="JavaScript/Reference/Global_Objects/String">String</a></th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.8.6</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>None (Harmony Proposal)</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code class="brush:js;">var <em>startsWith</em> = <em>str</em>.startsWith(<em>searchString</em> [, <em>position</em>])</code>;</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>searchString</code></dt>
  <dd>
    The characters to be searched for at the start of this string.</dd>
  <dt>
    <code>position</code></dt>
  <dd>
    The position in this string at which to begin searching for <code><var>searchString</var></code>; defaults to 0.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>This method lets you determine whether or not a string begins with another string.</p>
<h2 id="Examples" name="Examples">Examples</h2>
<pre class="brush:js;">
var str = "To be, or not to be, that is the question.";

alert(str.startsWith("To be"));         // true
alert(str.startsWith("not to be"));     // false
alert(str.startsWith("not to be", 10)); // true</pre>
<h2 id="See also" name="See also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Global_Objects/String/endsWith">String.endsWith</a></li>
</ul>
Revert to this revision