Checks if string ends with the given target string with optional position parameter
npx @tessl/cli install tessl/npm-lodash--endswith@4.2.00
# Lodash endsWith
1
2
Lodash endsWith is a string utility function that checks if a string ends with a given target string. It provides enhanced functionality beyond the native JavaScript String.prototype.endsWith() method by supporting a position parameter that allows checking if the string ends with the target at a specific position rather than just at the end.
3
4
## Package Information
5
6
- **Package Name**: lodash
7
- **Package Type**: npm
8
- **Language**: JavaScript
9
- **Installation**: `npm install lodash`
10
11
## Core Imports
12
13
```javascript
14
// Full lodash library
15
import _ from "lodash";
16
// Use as: _.endsWith(string, target, position)
17
```
18
19
```javascript
20
// Modular import (recommended)
21
import endsWith from "lodash/endsWith";
22
// Use as: endsWith(string, target, position)
23
```
24
25
For CommonJS:
26
27
```javascript
28
// Full library
29
const _ = require("lodash");
30
31
// Modular import
32
const endsWith = require("lodash/endsWith");
33
```
34
35
## Basic Usage
36
37
```javascript
38
import endsWith from "lodash/endsWith";
39
40
// Basic string ending check
41
endsWith("hello world", "world");
42
// => true
43
44
endsWith("hello world", "hello");
45
// => false
46
47
// Check with position parameter
48
endsWith("hello world", "hello", 5);
49
// => true (checks if "hello" ends at position 5)
50
51
// Edge cases
52
endsWith("test", "");
53
// => true (empty string always matches)
54
55
endsWith("", "test");
56
// => false
57
58
endsWith("abc", "c", 3);
59
// => true (position >= string length)
60
```
61
62
## Capabilities
63
64
### String Ending Detection
65
66
Checks if a string ends with the given target string, with optional position parameter for enhanced control.
67
68
```javascript { .api }
69
/**
70
* Checks if `string` ends with the given target string.
71
* @param {string} [string=''] - The string to search
72
* @param {string} [target] - The string to search for
73
* @param {number} [position=string.length] - The position to search from
74
* @returns {boolean} Returns `true` if `string` ends with `target`, else `false`
75
*/
76
function endsWith(string, target, position): boolean;
77
```
78
79
**Parameters:**
80
81
- `string` (string, optional): The string to search. `null` and `undefined` values are converted to empty string `''`, other values converted to string using coercion.
82
- `target` (string, optional): The string to search for. Gets converted to string using string coercion (`target + ''`).
83
- `position` (number, optional): The position to search from. Defaults to `string.length` if undefined. Gets clamped to range `[0, string.length]`.
84
85
**Returns:**
86
87
- `boolean`: Returns `true` if `string` ends with `target` at the specified position, else `false`.
88
89
**Behavior:**
90
91
- Performs string conversion on both `string` and `target` parameters
92
- Position parameter allows checking if string ends with target at a specific position rather than just at the end
93
- Position values are clamped to valid range: negative positions become 0, positions beyond string length become string length
94
- Empty target string always returns `true` regardless of position
95
- Uses efficient `indexOf` method for string matching
96
- Supports decimal position values (truncated to integer)
97
98
**Usage Examples:**
99
100
```javascript
101
import endsWith from "lodash/endsWith";
102
103
// Standard usage
104
endsWith("JavaScript", "Script");
105
// => true
106
107
endsWith("JavaScript", "Java");
108
// => false
109
110
// Position-based checking
111
endsWith("JavaScript", "Java", 4);
112
// => true (checks if "Java" ends at position 4)
113
114
endsWith("JavaScript", "Script", 6);
115
// => false ("Script" doesn't end at position 6)
116
117
// Edge cases
118
endsWith("test", "", 2);
119
// => true (empty target always matches)
120
121
endsWith("hello", "lo", 5);
122
// => true (position 5 equals string length)
123
124
endsWith("hello", "lo", 10);
125
// => true (position beyond length treated as string length)
126
127
// Type coercion examples
128
endsWith("123", 3);
129
// => true (number 3 converted to string "3")
130
131
endsWith(null, "");
132
// => true (null converted to empty string "")
133
134
endsWith(undefined, "");
135
// => true (undefined converted to empty string "")
136
```
137
138
## Error Handling
139
140
The `endsWith` function handles all parameter types gracefully through type coercion:
141
142
- Falsy `string` values are converted to their string representations
143
- Non-string `target` values are converted using string coercion
144
- Invalid `position` values are handled by clamping to valid range
145
- No exceptions are thrown for normal usage patterns
146
147
## Browser and Environment Support
148
149
- **Node.js**: All versions
150
- **Browsers**: All modern browsers (IE 9+)
151
- **Module Systems**: CommonJS, AMD, UMD, ES modules
152
- **Global**: Available as `_.endsWith` when lodash is loaded globally