Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>UPDATE (Nov 24th, 2015):</strong></p> <p>This answer is originally posted in the year 2010 (SIX years back.) so please take note of these insightful comments:</p> <ul> <li><p><a href="https://stackoverflow.com/users/570040/shauna">Shauna</a> - Update for Googlers - Looks like ECMA6 adds this function. The MDN article also shows a polyfill. <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith" rel="noreferrer">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith</a></p></li> <li><p><a href="https://stackoverflow.com/users/157247/t-j-crowder">T.J. Crowder</a> - Creating substrings isn't expensive on modern browsers; it may well have been in 2010 when this answer was posted. These days, the simple <code>this.substr(-suffix.length) === suffix</code> approach is fastest on Chrome, the same on IE11 as indexOf, and only 4% slower (fergetaboutit territory) on Firefox: <a href="http://jsperf.com/endswith-stackoverflow/14" rel="noreferrer">jsperf.com/endswith-stackoverflow/14</a> And faster across the board when the result is false: <a href="http://jsperf.com/endswith-stackoverflow-when-false" rel="noreferrer">jsperf.com/endswith-stackoverflow-when-false</a> <strong>Of course, with ES6 adding endsWith, the point is moot. :-)</strong></p></li> </ul> <hr> <p><strong>ORIGINAL ANSWER:</strong></p> <p>I know this is a year old question... but I need this too and I need it to work cross-browser so... <strong>combining everyone's answer and comments</strong> and simplifying it a bit:</p> <pre><code>String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; }; </code></pre> <ul> <li>Doesn't create a substring</li> <li>Uses native <code>indexOf</code> function for fastest results</li> <li>Skip unnecessary comparisons using the second parameter of <code>indexOf</code> to skip ahead</li> <li>Works in Internet Explorer</li> <li>NO Regex complications</li> </ul> <hr> <p>Also, if you don't like stuffing things in native data structure's prototypes, here's a standalone version:</p> <pre><code>function endsWith(str, suffix) { return str.indexOf(suffix, str.length - suffix.length) !== -1; } </code></pre> <hr> <p><strong>EDIT:</strong> As noted by @hamish in the comments, if you want to err on the safe side and check if an implementation has already been provided, you can just adds a <code>typeof</code> check like so:</p> <pre><code>if (typeof String.prototype.endsWith !== 'function') { String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload