Skip to content

Commit 9c1aa5c

Browse files
authored
[Term Entry] JavaScript Strings: .endsWith()
* Add documentation for the JavaScript .endsWith() method * Update endsWith.md * Update endsWith.md * Update endsWith.md ---------
1 parent d630c56 commit 9c1aa5c

File tree

1 file changed

+54
-0
lines changed
  • content/javascript/concepts/strings/terms/endsWith

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
Title: '.endsWith()'
3+
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));
54+
```

0 commit comments

Comments
 (0)