Skip to content

Instantly share code, notes, and snippets.

@cgcardona
Last active December 14, 2015 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cgcardona/5018788 to your computer and use it in GitHub Desktop.
Save cgcardona/5018788 to your computer and use it in GitHub Desktop.

Summary

Writing ECMAScript 6's proposed .contains, .startsWith, and .endsWith methods using good ole .indexOf.

.contains

'carlos'.contains('car');
// true
'carlos'.indexOf('car') >= 0;
// true

.startsWith

'carlos'.startsWith('car');
// true
'carlos'.indexOf('car') === 0;
// true

.endsWith

This one looks a little dirty to me because I needed to know the fname and arg in advance. Is there a way to do it so that I don't need those two variables?

'carlos'.endsWith('los');
// true
var fname = 'carlos';
var arg = 'los';
fname.indexOf(arg) == fname.length - arg.length;
// true
@cgcardona
Copy link
Author

:+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment