Skip to content

Commit 80cc15a

Browse files
fix: Quick Comparison Table & delete Key Takeways
1 parent 8653e78 commit 80cc15a

1 file changed

Lines changed: 18 additions & 64 deletions

File tree

data/blog/software-development/web-development/frontend/javascript/slice-vs-substring-vs-substr-complete-javascript-string-methods-comparison.mdx

Lines changed: 18 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: 'slice() vs substring() vs substr(): Complete JavaScript String Methods Comparison'
3-
date: '2026-01-16'
3+
date: '2026-01-12'
44
tags: ['frontend', 'javascript', 'software development', 'web development']
55
draft: false
66
summary: 'Comprehensive comparison of JavaScript string methods: String.prototype.slice(), String.prototype.substring(), and String.prototype.substr(). Learn the differences between slice(), substring(), and substr(), when to use each method, common pitfalls, and best practices with detailed code examples.'
@@ -31,19 +31,6 @@ This comprehensive guide will provide an in-depth exploration of each method, co
3131

3232
By the end of this article, you'll not only understand each method's behavior but also gain the expertise to choose the right method for your specific use case, write more robust code, and debug string manipulation issues effectively.
3333

34-
## Quick Comparison Table
35-
36-
Comparison of `String.prototype.slice()`, `String.prototype.substring()`, and `String.prototype.substr()` (commonly used as `slice()`, `substring()`, and `substr()`):
37-
38-
| Feature | `slice()` / `String.prototype.slice()` | `substring()` / `String.prototype.substring()` | `substr()` / `String.prototype.substr()` |
39-
|---------|-----------|---------------|------------|
40-
| **Syntax** | `slice(start, end)` | `substring(start, end)` | `substr(start, length)` |
41-
| **Second Parameter** | End index (exclusive) | End index (exclusive) | Length of substring |
42-
| **Negative Index** | Supported (counts from end) | Not supported (treated as 0) | Supported (start only) |
43-
| **Swaps Arguments** | No | Yes (if start > end) | No |
44-
| **Start > End** | Returns empty string | Swaps arguments | Works as expected |
45-
| **Status** | Recommended ✅ | Supported ⚠️ | Deprecated ❌ |
46-
4734
## Understanding `slice()` / `String.prototype.slice()`
4835

4936
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.
@@ -1191,53 +1178,20 @@ For developers who prefer interactive, text-based learning over video tutorials,
11911178

11921179
Understanding the differences between `String.prototype.slice()`, `String.prototype.substring()`, and `String.prototype.substr()` (commonly used as `slice()`, `substring()`, and `substr()`) is crucial for writing clean, maintainable JavaScript code. Throughout this comprehensive guide, we've explored the intricate behaviors of each method, their use cases, pitfalls, and best practices.
11931180

1194-
## Key Takeaways
1195-
1196-
### Method Comparison Summary
1197-
1198-
- **`slice()` / `String.prototype.slice()`****RECOMMENDED**:
1199-
- Supports negative indices (counts from end of string)
1200-
- Predictable behavior (no argument swapping)
1201-
- Modern ECMAScript standard (ES3+)
1202-
- Consistent with `Array.slice()` API
1203-
- End index is exclusive (not included in result)
1204-
- Returns empty string when start > end
1205-
- Part of official JavaScript specification
1206-
1207-
- **`substring()` / `String.prototype.substring()`** ⚠️ **LEGACY**:
1208-
- Does NOT support negative indices (treated as 0)
1209-
- Automatically swaps arguments when start > end (can hide bugs)
1210-
- Older method (ES1), still supported but less intuitive
1211-
- NaN values are treated as 0
1212-
- End index is exclusive (same as slice)
1213-
- May lead to unexpected results in edge cases
1214-
1215-
- **`substr()` / `String.prototype.substr()`****DEPRECATED**:
1216-
- Second parameter is LENGTH (not end index) - major difference!
1217-
- Supports negative start index only
1218-
- Deprecated and should NOT be used in new code
1219-
- Not part of ECMAScript standard (though widely supported)
1220-
- Can be replaced with `slice()` in all cases
1221-
- Confusing parameter semantics
1222-
1223-
### Critical Differences to Remember
1224-
1225-
1. **Parameter Semantics**:
1226-
- `slice(start, end)`: end is exclusive index
1227-
- `substring(start, end)`: end is exclusive index (but swaps if start > end)
1228-
- `substr(start, length)`: second param is LENGTH, not index!
1229-
1230-
2. **Negative Index Handling**:
1231-
- `slice()`: Negative indices count from end (intuitive)
1232-
- `substring()`: Negative indices become 0 (loses information)
1233-
- `substr()`: Negative start counts from end, but second param is still length
1234-
1235-
3. **Argument Order Behavior**:
1236-
- `slice()`: If start > end, returns empty string
1237-
- `substring()`: If start > end, automatically swaps arguments
1238-
- `substr()`: Length parameter makes this less relevant
1239-
1240-
4. **Type Coercion**:
1241-
- All methods coerce non-number types to numbers
1242-
- `slice()` and `substring()` handle undefined differently (slice defaults better)
1243-
- NaN becomes 0 in all methods
1181+
## Quick Comparison Table
1182+
1183+
Comparison of `String.prototype.slice()`, `String.prototype.substring()`, and `String.prototype.substr()` (commonly used as `slice()`, `substring()`, and `substr()`):
1184+
1185+
| Feature | `slice()` / `String.prototype.slice()` | `substring()` / `String.prototype.substring()` | `substr()` / `String.prototype.substr()` |
1186+
|---------|-----------|---------------|------------|
1187+
| **Syntax** | `slice(start, end)` | `substring(start, end)` | `substr(start, length)` |
1188+
| **Second Parameter** | End index (exclusive) | End index (exclusive) | Length of substring |
1189+
| **Negative Index** | Supported (counts from end) | Not supported (treated as 0) | Supported (start only) |
1190+
| **Swaps Arguments** | No | Yes (if start > end) | No |
1191+
| **Start > End** | Returns empty string | Swaps arguments | Works as expected |
1192+
| **ECMAScript Version** | ES3+ (modern standard) | ES1 (legacy) | Not part of standard |
1193+
| **Specification** | Part of official JavaScript spec | Part of official JavaScript spec | Not part of ECMAScript standard |
1194+
| **Array API Consistency** | Consistent with `Array.slice()` | N/A | N/A |
1195+
| **NaN Handling** | NaN treated as 0 | NaN treated as 0 | NaN treated as 0 |
1196+
| **Type Coercion** | Coerces non-numbers; undefined defaults better | Coerces non-numbers; undefined → 0 | Coerces non-numbers |
1197+
| **Status** | Recommended ✅ | Legacy ⚠️ | Deprecated ❌ |

0 commit comments

Comments
 (0)