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
+1-151Lines changed: 1 addition & 151 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,8 +28,6 @@ This comprehensive guide will provide an in-depth exploration of each method, co
28
28
-**Type coercion behavior** and how non-number values are handled
29
29
-**Unicode and special character handling** for international strings
30
30
-**Integration patterns** with other JavaScript string methods
31
-
-**Migration strategies** for legacy codebases
32
-
-**Interview preparation** with common questions and answers
33
31
34
32
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.
Here are typical interview questions about these methods:
1204
-
1205
-
#### Question 1: What's the difference between slice() and substring()?
1206
-
1207
-
**Answer:**
1208
-
-`slice()` supports negative indices (counts from end), `substring()` treats negatives as 0
1209
-
-`slice()` returns empty string if start > end, `substring()` swaps the arguments
1210
-
-`slice()` is more modern and recommended, `substring()` is legacy but still supported
1211
-
-`slice()` behavior is consistent with`Array.slice()`
1212
-
1213
-
#### Question 2: How would you get the last 3 characters of a string?
1214
-
1215
-
**Answer:**
1216
-
```javascript
1217
-
// Method 1: Using slice() with negative index (RECOMMENDED)
1218
-
const last3 = str.slice(-3);
1219
-
1220
-
// Method 2: Using slice() with calculated index
1221
-
const last3 = str.slice(str.length - 3);
1222
-
1223
-
// Method 3: Using substring() (less intuitive)
1224
-
const last3 = str.substring(str.length - 3);
1225
-
1226
-
// Method 4: Using substr() (deprecated, don't use)
1227
-
const last3 = str.substr(-3); // DON'T USE
1228
-
```
1229
-
1230
-
#### Question 3: What happens when you call substring(5, 2)?
1231
-
1232
-
**Answer:**
1233
-
The arguments are automatically swapped, so `substring(5, 2)` becomes `substring(2, 5)`. This returns the substring from index 2 to index 4 (exclusive of5).
1234
-
1235
-
In contrast, `slice(5, 2)` would return an empty string because `slice()` doesn't swap arguments.
1236
-
1237
-
### Working with Template Literals and Dynamic Strings
1238
-
1239
-
```javascript
1240
-
// Extracting from template literals
1241
-
const name = "John Doe";
1242
-
const age = 30;
1243
-
const template = `Hello, ${name}! You are ${age} years old.`;
0 commit comments