@@ -87,7 +87,7 @@ string.slice(startIndex, endIndex)
8787#### Basic Usage
8888
8989``` javascript
90- const str = " JavaScript" ; // Length: 10 characters
90+ const str = " JavaScript" ; // Length: 10 ( characters)
9191// Index: 0123456789
9292// J a v a S c r i p t
9393
@@ -156,7 +156,7 @@ console.log(str.slice(-8, -3)); // "vaScr"
156156#### Type Coercion
157157
158158``` javascript
159- const str = " JavaScript" ;
159+ const str = " JavaScript" ; // Length: 10
160160
161161// Example 1: String numbers are converted to numbers
162162console .log (str .slice (" 2" , " 5" )); // "vaS"
@@ -252,8 +252,7 @@ console.log(str.slice(4, -3)); // "Scri" (from index 4 to -3, which is positi
252252Understanding negative indices is crucial for effective string manipulation. Here's a detailed visual explanation:
253253
254254``` javascript
255- const str = " JavaScript" ;
256- // String length: 10 characters
255+ const str = " JavaScript" ; // Length: 10
257256
258257// Visual representation with positive indices:
259258// Character: J a v a S c r i p t
@@ -442,7 +441,7 @@ console.log(str.slice(-8, -3)); // "vaScr" (from -8 to -3, which is indic
442441#### Type Coercion (Same for ` substring() ` and ` slice() ` )
443442
444443``` javascript
445- const str = " JavaScript" ;
444+ const str = " JavaScript" ; // Length: 10
446445
447446// Example 1: String numbers are converted to numbers
448447console .log (str .substring (" 2" , " 5" )); // "vaS"
@@ -611,7 +610,7 @@ console.log(str.slice(4, -3)); // "Scri" (from index 4 to -3, which is
611610This is where ` substring() ` differs significantly from ` slice() ` :
612611
613612``` javascript
614- const str = " JavaScript" ;
613+ const str = " JavaScript" ; // Length: 10
615614
616615// Example 1: Arguments automatically swapped
617616console .log (str .substring (4 , 0 )); // "Java"
@@ -699,24 +698,51 @@ string.substr(startIndex, length)
699698### ` substr() ` Examples
700699
701700``` javascript
702- const str = " JavaScript" ;
701+ const str = " JavaScript" ; // Length: 10
703702
704703// Basic usage (NOTE: second param is LENGTH, not end index!)
705704console .log (str .substr (0 , 4 )); // "Java" (4 characters from index 0)
705+ // Comparison with slice():
706+ console .log (str .slice (0 , 4 )); // "Java" (same result - from index 0 to 4, exclusive)
707+ // substr(0, 4) uses length 4, slice(0, 4) uses end index 4 - same result in this case
708+
706709console .log (str .substr (4 , 6 )); // "Script" (6 characters from index 4)
707- console .log (str .substr (4 )); // "Script" (to end)
710+ // Comparison with slice():
711+ console .log (str .slice (4 , 4 + 6 )); // "Script" (from index 4 to 10, exclusive)
712+ // substr(4, 6) uses length 6, slice(4, 10) uses end index 10 - same result
713+
714+ console .log (str .substr (4 )); // "Script" (to end - length omitted)
715+ // Comparison with slice():
716+ console .log (str .slice (4 )); // "Script" (from index 4 to end - same result)
708717
709718// Negative start index
710719console .log (str .substr (- 6 )); // "Script" (last 6 characters)
711- console .log (str .substr (- 6 , 3 )); // "Scr" (3 characters starting from -6)
720+ // Comparison with slice():
721+ console .log (str .slice (- 6 )); // "Script" (from index -6 to end - same result)
722+
723+ console .log (str .substr (- 6 , - 1 )); // "" (empty string - negative length treated as 0)
724+ // Comparison with slice():
725+ console .log (str .slice (- 6 , - 1 )); // "Scrip" (from index -6 to -1, exclusive)
726+ // substr(-6, -1) uses negative length (treated as 0), slice(-6, -1) uses negative end index
712727
713728// Edge cases
714729console .log (str .substr (0 , 0 )); // "" (length 0)
715- console .log (str .substr (0 , 20 )); // "JavaScript" (entire string, length capped)
730+ // Comparison with slice():
731+ console .log (str .slice (0 , 0 )); // "" (from index 0 to 0, exclusive - same result)
732+
733+ console .log (str .substr (0 , 100 )); // "JavaScript" (entire string, length capped)
734+ // Comparison with slice():
735+ console .log (str .slice (0 , 100 )); // "JavaScript" (entire string, end index clamped - same result)
736+
716737console .log (str .substr (20 )); // "" (start beyond string length)
738+ // Comparison with slice():
739+ console .log (str .slice (20 )); // "" (start beyond string length - same result)
717740
718- // Negative start with length
719- console .log (str .substr (- 4 , 2 )); // "ip" (2 characters from 4th from end)
741+ // Negative start with negative length
742+ console .log (str .substr (- 4 , - 1 )); // "" (empty string - negative length treated as 0)
743+ // Comparison with slice():
744+ console .log (str .slice (- 4 , - 1 )); // "rip" (from index -4 to -1, exclusive)
745+ // substr(-4, -1) uses negative length (treated as 0), slice(-4, -1) uses negative end index
720746```
721747
722748## Side-by-Side Comparison
@@ -1045,7 +1071,7 @@ console.log(str.slice(5, 0)); // "" (empty string)
10451071### Pitfall 2: Using Negative Indices with substring()
10461072
10471073``` javascript
1048- const str = " JavaScript" ;
1074+ const str = " JavaScript" ; // Length: 10
10491075
10501076// This doesn't work as you might expect
10511077console .log (str .substring (- 3 )); // "JavaScript" (entire string, not last 3)
0 commit comments