You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: 'Checks if a string ends with a specified substring, returning true if it does and false otherwise.'
4
+
Subjects:
5
+
- 'Computer Science'
6
+
- 'Web Development'
7
+
Tags:
8
+
- 'Methods'
9
+
- 'Strings'
10
+
CatalogContent:
11
+
- 'introduction-to-javascript'
12
+
- 'paths/front-end-engineer-career-path'
13
+
---
14
+
15
+
The **`.endsWith()`** JavaScript string method checks whether a string ends with a given substring, returning `true` if it does and `false` otherwise. It returns `true` when the specified string is empty (`""`). The method is case-sensitive.
16
+
17
+
## Syntax
18
+
19
+
```pseudo
20
+
string.endsWith(searchString, endPosition);
21
+
```
22
+
23
+
**Parameters:**
24
+
25
+
-`searchString`: The characters to search for at the end of `string`.
26
+
-`length` (optional): If provided, it considers only the first [`length`](https://www.codecademy.com/resources/docs/javascript/strings/length) characters of the string.
27
+
28
+
## Example
29
+
30
+
In this example, the `.endsWith()` method checks if a string ends with a given substring, optionally considering only a portion of the string based on the provided length:
31
+
32
+
```js
33
+
console.log('Hello, World! This is JavaScript.'.endsWith('JavaScript.'));
34
+
35
+
console.log('Hello, World! This is JavaScript.'.endsWith('JavaScript'));
36
+
37
+
console.log('Hello, World! This is JavaScript.'.endsWith('World', 12));
38
+
```
39
+
40
+
The output of this code is:
41
+
42
+
```shell
43
+
true
44
+
false
45
+
true
46
+
```
47
+
48
+
## Codebyte Example
49
+
50
+
This codebyte example is runnable and checks whether a string, up to the specified position, ends with the given search string:
51
+
52
+
```codebyte/javascript
53
+
console.log('Does Codecademy end with my?'.endsWith('my', 15));
0 commit comments