--- title: String.prototype.endsWith() slug: Web/JavaScript/Reference/Global_Objects/String/endsWith translation_of: Web/JavaScript/Reference/Global_Objects/String/endsWith ---
The endsWith() 메서드를 사용하여 어떤 문자열에서 특정 문자열로 끝나는지를 확인할 수 있으며, 그 결과를 true 혹은 false로 반환한다.
str.endsWith(searchString[, length])
searchStringlength문자열의 끝이 주어진 문자열로 끝나면 true, 그렇지 않다면 false
여러분은 이 메서드를 사용하여 어떤 문자열이 특정 문자열로 끝나는지를 확인할 수 있습니다.
endsWith() 사용하기var str = 'To be, or not to be, that is the question.';
console.log(str.endsWith('question.')); // true
console.log(str.endsWith('to be')); // false
console.log(str.endsWith('to be', 19)); // true
이 메서드는 ECMAScript 6 규격에 포함되었습니다만 아직까지는 모든 JavaScrpt가 이 기능을 지원하고 있지는 않습니다. 하지만 여러분은 String.prototype.endsWith() 메서드를 다음과 같이 쉽게 polyfill 할 수 있습니다:
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('ES6', '#sec-string.prototype.endswith', 'String.prototype.endsWith')}} | {{Spec2('ES6')}} | Initial definition. |
| {{SpecName('ESDraft', '#sec-string.prototype.endswith', 'String.prototype.endsWith')}} | {{Spec2('ESDraft')}} |
| Feature | Chrome | Firefox (Gecko) | Edge | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|---|
| Basic support | {{CompatChrome("41")}} | {{CompatGeckoDesktop("17")}} | {{CompatVersionUnknown}} | {{CompatNo}} | {{CompatNo}} | {{CompatSafari("9")}} |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | {{CompatNo}} | {{CompatChrome("36")}} | {{CompatGeckoMobile("17")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |