slice, substring & at() in JavaScript
Three slicers with subtly different contracts: slice(start, end) supports negatives (from the end) and swaps nothing — the modern default; substring(start, end) clamps negatives to 0 and swaps reversed bounds (legacy quirks to know, not use); substr(start, length) is deprecated legacy. New code uses slice almost exclusively.
All return new strings without touching the original, and end-exclusive ranges compose with indexOf results directly. For grapheme-correct slicing (emoji), operate on [...s] arrays instead of raw indexes.
slice is the default
s.slice(1, 4), s.slice(2), s.slice(-3) — negatives count from the end, end-exclusive, no mutation.
substring quirks
Clamps negatives to 0 and swaps reversed args — legacy behavior to recognize, not to write.
substr is deprecated
Length-based second arg, still found in old code — replace with slice on sight.
Mind the units
All three count UTF-16 units — emoji-adjacent slicing needs code-point arrays first.
let s = "hello";console.log(s.slice(1, 4));console.log(s.slice(-2));Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.