JavaScript String slice() is a built-in function that extracts a portion of a string and returns it as a new string object, without modifying the original string.
const main_string = "Corporation" console.log(main_string.slice(0, 5)); // Output: Corpo
In this code, we passed the starting index and the ending index to the slice() method.
The string starts with index 0 and extends up to index 4. Note that the end index is not included; it is just up to 4. Meaning 0 to 4. This is the basic overview of the function.
Syntax
string.slice(start_index, end_index)
Parameters
Argument | Description |
start_index (required, integer) |
It represents a zero-based index at which extraction begins. If it is negative, it is treated as string.length + beginIndex. |
end_index (optional, integer) | It represents the zero-based index before which extraction ends (exclusive; the character at endIndex is not included). |
Omit end index (extract to end)
If you omit the end index from the slice() function, it will take a start point and extract up to the end of the string. It is ideal for suffix extraction.
const main_string = "Corporation" console.log(main_string.slice(5)); // Output: ration
In this case, the starting point is index number 5 up to the last character’s index, and hence the output is “ration”.
Negative start index (Counting from the end)
If the starting index is negative, JavaScript counts from the end of the string. In this case, the last character has an index of -1, the second-to-last character is -2, and so on.
Therefore, if the starting index is -8, the extraction will begin at the 8th character from the end of the string and continue up to the last character (index -1).
const main_string = "Corporation" console.log(main_string.slice(-8)); // Output: poration
In this code, the character “n” has an index of -1, and the character “p” has an index of -8. So, -8 to -1 extracts the portion “poration”.
Negative end index
This is somewhat tricky. Let’s say your starting index is 0 and your ending index is -7. That means, you start extracting from the 0th index up to the ending index -7. For a negative index, you count from the end, meaning -1, -2,..-7 characters. So, 0 to -7.
const main_string = "Corporation" console.log(main_string.slice(0, -7)); // Output: Corp
In this code, the starting index is 0, corresponding to “C”, and the ending index is -7, corresponding to “o”.
So, it excludes the last character “o”, we will have the extracted string “Corp”.
Both negative indexes
What if both indices are negative? Well, in that case, you need to count both indices from the end.
Let’s say we have a start index of -6 and an end index of -2; it will extract between these two points.
const main_string = "Corporation" console.log(main_string.slice(-6, -2)); // Output: rati
In this code, the starting index is -6, which means that if you count backwards from index -1, it will be a character “r”, and -2 translates to “o”, but that is excluded, so “i” would be the last character of the extracted string and hence the output “rati”.
Slicing the last character from the string
To slice the last character in JavaScript, use negative indexing. For example, pass the -1 argument as the start index to the slice() method, and it will return the last character.
const main_string = "Corporation" last_character = main_string.slice(-1) console.log(last_character) // Output: n
Slicing the last three characters from the string
For slicing the last three characters, pass -3 as a start index to the slice() method.
const main_string = "Corporation" last_three_chars = main_string.slice(-3) console.log(last_three_chars) // Output: ion
The output clearly shows that the last three characters are “ion” from the input string.
Empty string
If the input string is empty, the output string will also be empty, because there is nothing to slice.
const empty_string = "" sliced_str = empty_string.slice(-3) console.log(sliced_str) // Output:
As expected, the output is an empty string.
That’s it!