@@ -243,24 +243,12 @@ console.log(str.slice(-3, -6)); // "" (empty string)
243243// End: -6 becomes 10 - 6 = 4
244244// Result: str.slice(7, 4) which is empty (7 > 4)
245245
246- // Case 9: Positive start, negative end
247- console .log (str .slice (0 , - 1 )); // "JavaScrip" (all except last character)
248- // Explanation:
249- // Start: 0
250- // End: -1 becomes 10 - 1 = 9
251- // Result: str.slice(0, 9) which gives "JavaScrip" (indices 0-8)
246+ // Case 9: Positive start (not 0), negative end
252247console .log (str .slice (4 , - 3 )); // "Scri" (from index 4 to -3, which is position 7)
253248// Explanation:
254249// Start: 4
255250// End: -3 becomes 10 - 3 = 7
256251// Result: str.slice(4, 7) which gives "Scri" (indices 4-6)
257-
258- // Case 10: Negative start, positive end
259- console .log (str .slice (- 4 , 10 )); // "ript" (last 4 characters)
260- // Explanation:
261- // Start: -4 becomes 10 - 4 = 6
262- // End: 10 (clamped to string.length)
263- // Result: "ript" (indices 6, 7, 8, 9)
264252```
265253
266254### ` slice() ` with Negative Indices Explained (Visual Guide)
@@ -604,17 +592,7 @@ console.log(str.slice(-3, -6)); // "" (empty string)
604592// Both return empty, but for different reasons:
605593// substring() treats both negatives as 0, slice() converts them and finds 7 > 4
606594
607- // Case 9: Positive start, negative end
608- console .log (str .substring (0 , - 1 )); // "" (empty string)
609- // Step-by-step:
610- // 1. startIndex = 0
611- // 2. endIndex = -1 → treated as 0
612- // 3. Check: 0 > 0? NO → No swap
613- // 4. Extract: indices from 0 to 0 (exclusive) → ""
614- // Equivalent to: str.substring(0, 0)
615- // Comparison with slice():
616- console .log (str .slice (0 , - 1 )); // "JavaScrip" (from index 0 to -1, which is position 9)
617- // slice() correctly interprets negative end, substring() treats negative end as 0
595+ // Case 9: Positive start (not 0), negative end
618596console .log (str .substring (4 , - 3 )); // "Java"
619597// Step-by-step:
620598// 1. startIndex = 4
@@ -626,18 +604,6 @@ console.log(str.substring(4, -3)); // "Java"
626604// Comparison with slice():
627605console .log (str .slice (4 , - 3 )); // "Scri" (from index 4 to -3, which is position 7)
628606// slice() correctly interprets negative end (7), substring() treats negative end as 0 and swaps
629-
630- // Case 10: Negative start, positive end
631- console .log (str .substring (- 4 , 10 )); // "JavaScript" (entire string)
632- // Step-by-step:
633- // 1. startIndex = -4 → treated as 0
634- // 2. endIndex = 10
635- // 3. Check: 0 > 10? NO → No swap
636- // 4. Extract: indices 0-9 → entire string "JavaScript"
637- // Equivalent to: str.substring(0, 10)
638- // Comparison with slice():
639- console .log (str .slice (- 4 , 10 )); // "ript" (from index -4 which is position 6, to 10)
640- // slice() correctly interprets negative start (6), substring() treats negative start as 0
641607```
642608
643609#### Argument Swapping Behavior (The Quirky Part!)
0 commit comments