Skip to content

Commit dc3b515

Browse files
fix: reorder Understanding substr()
1 parent fe8734f commit dc3b515

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

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

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,7 @@ function safeExtract(str, index) {
663663
}
664664
```
665665

666-
667-
## Understanding substr() / String.prototype.substr() (Deprecated)
666+
## Understanding `substr()` / `String.prototype.substr()` (Deprecated)
668667

669668
The `String.prototype.substr()` method (commonly called `substr()`) returns a portion of the string, starting at the specified position and extending for a given number of characters. **Important: `substr()` is deprecated and should not be used in new code.**
670669

@@ -679,37 +678,14 @@ string.substr(startIndex, length)
679678
- **startIndex**: The index of the first character to include in the returned substring
680679
- **length** (optional): The number of characters to extract. If omitted, extracts to the end of the string.
681680

682-
### Key Features of substr()
681+
### Key Features of `substr()`
683682

684683
1. **Second Parameter is Length**: Unlike `slice()` and `substring()`, the second parameter is the length, not the end index
685684
2. **Supports Negative Start**: Negative start index counts from the end
686685
3. **Deprecated**: Should not be used in new code
687686
4. **Inconsistent with Other Methods**: Different parameter meaning causes confusion
688687

689-
### substr() Examples
690-
691-
```javascript
692-
const str = "JavaScript";
693-
694-
// Basic usage (NOTE: second param is LENGTH, not end index!)
695-
console.log(str.substr(0, 4)); // "Java" (4 characters from index 0)
696-
console.log(str.substr(4, 6)); // "Script" (6 characters from index 4)
697-
console.log(str.substr(4)); // "Script" (to end)
698-
699-
// Negative start index
700-
console.log(str.substr(-6)); // "Script" (last 6 characters)
701-
console.log(str.substr(-6, 3)); // "Scr" (3 characters starting from -6)
702-
703-
// Edge cases
704-
console.log(str.substr(0, 0)); // "" (length 0)
705-
console.log(str.substr(0, 20)); // "JavaScript" (entire string, length capped)
706-
console.log(str.substr(20)); // "" (start beyond string length)
707-
708-
// Negative start with length
709-
console.log(str.substr(-4, 2)); // "ip" (2 characters from 4th from end)
710-
```
711-
712-
### Why substr() is Deprecated
688+
### Why `substr()` is Deprecated
713689

714690
```javascript
715691
// substr() has been deprecated because:
@@ -735,6 +711,29 @@ const start = str.length + (-6); // Calculate start index
735711
console.log(str.slice(start, start + 3)); // "Scr" (recommended)
736712
```
737713

714+
### `substr()` Examples
715+
716+
```javascript
717+
const str = "JavaScript";
718+
719+
// Basic usage (NOTE: second param is LENGTH, not end index!)
720+
console.log(str.substr(0, 4)); // "Java" (4 characters from index 0)
721+
console.log(str.substr(4, 6)); // "Script" (6 characters from index 4)
722+
console.log(str.substr(4)); // "Script" (to end)
723+
724+
// Negative start index
725+
console.log(str.substr(-6)); // "Script" (last 6 characters)
726+
console.log(str.substr(-6, 3)); // "Scr" (3 characters starting from -6)
727+
728+
// Edge cases
729+
console.log(str.substr(0, 0)); // "" (length 0)
730+
console.log(str.substr(0, 20)); // "JavaScript" (entire string, length capped)
731+
console.log(str.substr(20)); // "" (start beyond string length)
732+
733+
// Negative start with length
734+
console.log(str.substr(-4, 2)); // "ip" (2 characters from 4th from end)
735+
```
736+
738737
## Side-by-Side Comparison
739738

740739
Let's compare all three methods with the same inputs to see how they differ:

0 commit comments

Comments
 (0)