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
Copy file name to clipboardExpand all lines: data/blog/software-development/web-development/frontend/javascript/slice-vs-substring-vs-substr-complete-javascript-string-methods-comparison.mdx
+35-35Lines changed: 35 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,7 +46,7 @@ Comparison of `String.prototype.slice()`, `String.prototype.substring()`, and `S
46
46
|**Start > End**| Returns empty string | Swaps arguments | Works as expected |
The `String.prototype.slice()` method (commonly called `slice()`) extracts a section of a string and returns it as a new string, without modifying the original string. It's the most modern and recommended method for extracting substrings in JavaScript. `slice()` was introduced in ECMAScript 3 and is part of the official JavaScript specification, making it the standard choice for string extraction operations.
-**startIndex**: The zero-based index at which to begin extraction. Can be negative (counts from end). If negative, it's converted to `string.length + startIndex`. If omitted or undefined, defaults to 0.
62
-
-**endIndex** (optional): The zero-based index **before which** to end extraction (exclusive). Can be negative (counts from end). If negative, it's converted to `string.length + endIndex`. If omitted or undefined, defaults to `string.length`. If`endIndex` is greater than `string.length`, it's clamped to `string.length`.
61
+
-**`startIndex`**: The zero-based index at which to begin extraction. Can be negative (counts from end). If negative, it's converted to `string.length + startIndex`. If undefined, defaults to 0. If greater than `string.length`, it's clamped to `string.length`.
62
+
-**`endIndex`** (optional): The zero-based index **before which** to end extraction (exclusive). Can be negative (counts from end). If negative, it's converted to `string.length + endIndex`. If omitted or undefined, defaults to `string.length`. If greater than `string.length`, it's clamped to `string.length`.
63
63
64
-
### Key Features of slice()
64
+
### Key Features of `slice()`
65
65
66
66
1.**Supports Negative Indices**: Negative indices count backwards from the end of the string, making it intuitive to extract from the end
67
67
2.**No Argument Swapping**: If `start > end`, returns an empty string (predictable behavior)
68
68
3.**Consistent Behavior**: Predictable and intuitive behavior across all scenarios
69
-
4.**Similar to Array.slice()**: Works identically to `Array.prototype.slice()`, providing consistency across JavaScript APIs
69
+
4.**Similar to `Array.slice()`**: Works identically to `Array.prototype.slice()`, providing consistency across JavaScript APIs
70
70
5.**Immutable**: Never modifies the original string, always returns a new string
71
71
6.**Clamps Indices**: Automatically handles out-of-bounds indices gracefully
72
72
7.**Exclusive End Index**: The end index is exclusive (not included in the result), which is consistent with many programming languages
The `String.prototype.substring()` method (commonly called `substring()`) returns a part of the string between the start and end indexes, or to the end of the string. Unlike `slice()`, `substring()` has some quirky behaviors that can lead to unexpected results, making it less intuitive for many developers. `substring()` was one of the earliest string methods in JavaScript (since ES1) and has legacy behaviors that were kept for backward compatibility.
-**startIndex**: The zero-based index at which to begin extraction. If negative, NaN, or undefined, it's treated as 0. If greater than `string.length`, it's treated as `string.length`.
317
-
-**endIndex** (optional): The zero-based index before which to end extraction (exclusive). If negative, NaN, or undefined, it's treated as 0. If omitted, defaults to `string.length`. If greater than `string.length`, it's treated as `string.length`.
316
+
-**`startIndex`**: The zero-based index at which to begin extraction. If undefined, negative, or NaN, it's treated as 0. If greater than `string.length`, it's treated as `string.length`.
317
+
-**`endIndex`** (optional): The zero-based index **before which** to end extraction (exclusive). If omitted, defaults to `string.length`. If undefined, negative, or NaN, it's treated as 0. If greater than `string.length`, it's treated as `string.length`.
318
318
319
319
### Internal Algorithm
320
320
321
321
The `substring()` method processes indices differently from `slice()`:
322
322
323
-
1.**Convert startIndex**:
323
+
1.**Convert `startIndex`**:
324
324
- If negative, NaN, or undefined: convert to 0
325
325
- If greater than `string.length`: convert to `string.length`
326
326
- Otherwise: use as-is
327
327
328
-
2.**Convert endIndex**:
328
+
2.**Convert `endIndex`**:
329
329
- If omitted: use `string.length`
330
330
- If negative, NaN, or undefined: convert to 0
331
331
- If greater than `string.length`: convert to `string.length`
0 commit comments