On this page
String.slice
Prev: String | Up: String | Next: split
Rel: String, String, RegExp & Intl Guide
Ref: MDN Web Docs — String.slice
Tags: #public #web #javascript #builtins #string #tier/core
Signature & Overview
str.slice(startIndex, endIndex?)
Extracts a substring without mutating str; supports negative indices from the end.
Notes & Pitfalls
- Prefer
slice()oversubstring()and deprecatedsubstr(): Supports negative indices cleanly (str.slice(-3)) without swapping arguments.
Example
console.assert("JavaScript".slice(0, 4) === "Java");
console.assert("JavaScript".slice(-6) === "Script");
Linked from
Related notes
- tuts/javascript/builtins/String/split Linked here; links to this page; shared tags: #builtins, #javascript, #string, #tier, #web
- tuts/javascript/builtins/String Linked here; links to this page; shared tags: #builtins, #javascript, #string, #web
- tuts/javascript/builtins/String/substr links to this page; shared tags: #builtins, #javascript, #string, #tier, #web
- tuts/javascript/builtins/string_regexp_and_intl Linked here; shared tags: #builtins, #javascript, #web
- tuts/javascript/builtins/String/includes shared tags: #builtins, #javascript, #string, #tier, #web
- tuts/javascript/builtins/String/matchAll shared tags: #builtins, #javascript, #string, #tier, #web
- tuts/javascript/builtins/String/replaceAll shared tags: #builtins, #javascript, #string, #tier, #web
- tuts/javascript/builtins/String/trim shared tags: #builtins, #javascript, #string, #tier, #web
- tuts/javascript/builtins/Array/at shared tags: #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array/filter shared tags: #builtins, #javascript, #tier, #web