mozilla

Revision 507489 of String.prototype.lastIndexOf()

  • Revision slug: Web/JavaScript/Reference/Global_Objects/String/lastIndexOf
  • Revision title: String.prototype.lastIndexOf()
  • Revision id: 507489
  • Created:
  • Creator: fscholz
  • Is current revision? No
  • Comment cleanup

Revision Content

{{JSRef("Global_Objects", "String")}}

Summary

The lastIndexOf() method returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found. The calling string is searched backward, starting at fromIndex.

Syntax

str.lastIndexOf(searchValue[, toIndex])

Parameters

searchValue
A string representing the value to search for.
toIndex
The location within the calling string to end the search at, indexed from left to right. It can be any integer between 0 and the length of the string. The default value is the length of the string.

Description

Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character is stringName.length - 1.

"canal".lastIndexOf("a")   // returns 3
"canal".lastIndexOf("a",2) // returns 1
"canal".lastIndexOf("a",0) // returns -1
"canal".lastIndexOf("x")   // returns -1

Case-sensitivity

The lastIndexOf method is case sensitive. For example, the following expression returns -1:

"Blue Whale, Killer Whale".lastIndexOf("blue"); // returns -1

Examples

Example: Using indexOf and lastIndexOf

The following example uses indexOf and lastIndexOf to locate values in the string "Brave new world".

var anyString = "Brave new world";

console.log("The index of the first w from the beginning is " + anyString.indexOf("w"));
// Displays 8
console.log("The index of the first w from the end is " + anyString.lastIndexOf("w")); 
// Displays 10

console.log("The index of 'new' from the beginning is " + anyString.indexOf("new"));   
// Displays 6
console.log("The index of 'new' from the end is " + anyString.lastIndexOf("new"));
// Displays 6

Specifications

Specification Status Comment
ECMAScript 1st Edition. Standard Initial definition.
{{SpecName('ES5.1', '#sec-15.5.4.8', 'String.prototype.lastIndexOf')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-string.prototype.lastindexof', 'String.prototype.lastIndexOf')}} {{Spec2('ES6')}}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

See also

  • {{jsxref("String.prototype.charAt()")}}
  • {{jsxref("String.prototype.indexOf()")}}
  • {{jsxref("String.prototype.split()")}}
  • {{jsxref("Array.prototype.indexOf()")}}

Revision Source

<p>{{JSRef("Global_Objects", "String")}}</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The <code><strong>lastIndexOf()</strong></code> method returns the index within the calling <code>String</code> object of the last occurrence of the specified value, or -1 if not found. The calling string is searched backward, starting at <code>fromIndex</code>.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<code><em>str</em>.lastIndexOf(<em>searchValue</em>[, <em>toIndex</em>])</code></pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
 <dt>
  <code>searchValue</code></dt>
 <dd>
  A string representing the value to search for.</dd>
 <dt>
  <code>toIndex</code></dt>
 <dd>
  The location within the calling string to end the search at, indexed from left to right. It can be any integer between 0 and the length of the string. The default value is the length of the string.</dd>
</dl>
<h2 id="Description" name="Description">Description</h2>
<p>Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character is <code>stringName.length - 1</code>.</p>
<pre class="brush: js">
"canal".lastIndexOf("a")   // returns 3
"canal".lastIndexOf("a",2) // returns 1
"canal".lastIndexOf("a",0) // returns -1
"canal".lastIndexOf("x")   // returns -1
</pre>
<h3 id="Example:_indexOf_and_case-sensitivity" name="Example:_indexOf_and_case-sensitivity">Case-sensitivity</h3>
<p>The <code>lastIndexOf</code> method is case sensitive. For example, the following expression returns -1:</p>
<pre class="brush:js">
"Blue Whale, Killer Whale".lastIndexOf("blue"); // returns -1</pre>
<h2 id="Examples" name="Examples">Examples</h2>
<h3 id="Example:_Using_indexOf_and_lastIndexOf" name="Example:_Using_indexOf_and_lastIndexOf">Example: Using <code>indexOf</code> and <code>lastIndexOf</code></h3>
<p>The following example uses <code>indexOf</code> and <code>lastIndexOf</code> to locate values in the string "<code>Brave new world</code>".</p>
<pre class="brush:js">
var anyString = "Brave new world";

console.log("The index of the first w from the beginning is " + anyString.indexOf("w"));
// Displays 8
console.log("The index of the first w from the end is " + anyString.lastIndexOf("w")); 
// Displays 10

console.log("The index of 'new' from the beginning is " + anyString.indexOf("new"));   
// Displays 6
console.log("The index of 'new' from the end is " + anyString.lastIndexOf("new"));
// Displays 6
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition.</td>
   <td>Standard</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.5.4.8', 'String.prototype.lastIndexOf')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-string.prototype.lastindexof', 'String.prototype.lastIndexOf')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li>{{jsxref("String.prototype.charAt()")}}</li>
 <li>{{jsxref("String.prototype.indexOf()")}}</li>
 <li>{{jsxref("String.prototype.split()")}}</li>
 <li>{{jsxref("Array.prototype.indexOf()")}}</li>
</ul>
<!--languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String/lastIndexOf", "ja": "ja/JavaScript/Reference/Global_Objects/String/lastIndexOf", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/lastIndexOf" } )-->
Revert to this revision