aboutsummaryrefslogtreecommitdiff
path: root/files/uk/web/javascript/reference/global_objects/string/endswith/index.html
blob: e387abf56cc8daae0617d816d458f74697f0838d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
title: String.prototype.endsWith()
slug: Web/JavaScript/Reference/Global_Objects/String/endsWith
tags:
  - JavaScript
  - String
  - метод
translation_of: Web/JavaScript/Reference/Global_Objects/String/endsWith
---
<div>{{JSRef}}</div>

<p><span class="seoSummary">Метод <strong><code>endsWith()</code></strong> визначає, чи завершується рядок символами вказаного рядка, повертаючи, відповідно, <code>true</code> чи <code>false</code>.</span></p>

<div>{{EmbedInteractiveExample("pages/js/string-endswith.html")}}</div>



<h2 id="Синтаксис">Синтаксис</h2>

<pre class="syntaxbox"><var>str</var>.endsWith(<var>searchString</var>[, <var>length</var>])</pre>

<h3 id="Параметри">Параметри</h3>

<dl>
 <dt><code><var>searchString</var></code></dt>
 <dd>Символи, які шукатимуться в кінці рядка <code><var>str</var></code>.</dd>
 <dt><code><var>length</var></code> {{optional_inline}}</dt>
 <dd>Якщо наданий, використовується як довжина <code><var>str</var></code>. За замовчуванням дорівнює <code><var>str</var>.length</code>.</dd>
</dl>

<h3 id="Значення_що_повертається">Значення, що повертається</h3>

<p><strong><code>true</code></strong>, якщо надані символи знайдені в кінці рядка; інакше, <strong><code>false</code></strong>.</p>

<h2 id="Опис">Опис</h2>

<p>Цей метод дозволяє визначити, чи завершується рядок іншим рядком. Цей метод чутливий до регістру.</p>

<h2 id="Приклади">Приклади</h2>

<h3 id="Використання_endsWith">Використання <code>endsWith()</code></h3>

<pre class="brush: js">let str = 'Питання в тому: бути чи не бути.'

console.log(str.endsWith('бути.'))  // true
console.log(str.endsWith('Питання'))      // false
console.log(str.endsWith('Питання', 7))  // true
</pre>

<h2 id="Поліфіл">Поліфіл</h2>

<p>Цей метод був доданий до специфікації ECMAScript 6 та може поки не бути доступним в усіх реалізаціях JavaScript. Однак, ви можете створити поліфіл <code>String.prototype.endsWith()</code> за допомогою наступного коду:</p>

<pre class="brush: js">if (!String.prototype.endsWith) {
	String.prototype.endsWith = function(search, this_len) {
		if (this_len === undefined || this_len &gt; this.length) {
			this_len = this.length;
		}
		return this.substring(this_len - search.length, this_len) === search;
	};
}
</pre>

<h2 id="Специфікації">Специфікації</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Специфікація</th>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-string.prototype.endswith', 'String.prototype.endsWith')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Сумісність_з_веб-переглядачами">Сумісність з веб-переглядачами</h2>

<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>

<p>{{Compat("javascript.builtins.String.endsWith")}}</p>

<h2 id="Див._також">Див. також</h2>

<ul>
 <li>{{jsxref("String.prototype.startsWith()")}}</li>
 <li>{{jsxref("String.prototype.includes()")}}</li>
 <li>{{jsxref("String.prototype.indexOf()")}}</li>
 <li>{{jsxref("String.prototype.lastIndexOf()")}}</li>
</ul>