String.slice()
Extracts a substring using start/end indexes (supports negatives).
string.slice(start?, end?)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Extracts a substring using start/end indexes (supports negatives).
The slice() method extracts a section of a string and returns it as a new string, maintaining the immutability of the original primitive. It accepts two optional parameters: a zero-based start index and an exclusive end index. A key feature is its support for negative integers; if an index is negative, it is treated as str.length + index, allowing for intuitive suffix extraction. Performance is highly optimized in modern engines; while technically O(k) where k is the length of the substring, many engines use 'sliced strings' or 'slices' that share the underlying buffer of the original string to save memory and time. Unlike String.prototype.substring(), slice() does not swap arguments if the start index is greater than the end index; instead, it returns an empty string. If the start index is greater than or equal to the string length, an empty string is also returned.
Quick reference
Syntax
string.slice(start?, end?)
Inputs
Parameters
See it in practice
Examples
Basic Substring Extraction
const text = 'JavaScript';
const result = text.slice(0, 4);"Java"
Extracts characters from index 0 up to, but not including, index 4.
Using Negative Indices for Suffixes
const filename = 'image.png';
const extension = filename.slice(-3);"png"
A negative index starts counting from the end of the string, making it easy to grab the last N characters.
Extracting to the End
const phrase = 'Mozilla Web Docs';
const result = phrase.slice(8);"Web Docs"
If the second argument is omitted, slice() extracts everything from the start index to the very end of the string.
Debug faster
Common Errors
LogicError
Cause: Confusing the end index with a length parameter (similar to the deprecated substr method).
Fix: Remember that the second parameter is the index where extraction stops, not the number of characters to extract.
'Hello'.slice(1, 2) // returns 'e', not 'el'LogicError
Cause: Assuming slice() will swap arguments if the start index is larger than the end index.
Fix: Ensure the start index is smaller than the end index, or use String.prototype.substring() if argument swapping behavior is required.
'Modern'.slice(4, 1) // returns '' (empty string)Runtime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Extracts a substring using start/end indexes (supports negatives).
start: Start index (inclusive). Default 0. end: End index (exclusive). Default string.length.
LogicError: Remember that the second parameter is the index where extraction stops, not the number of characters to extract. LogicError: Ensure the start index is smaller than the end index, or use String.prototype.substring() if argument swapping behavior is required.