Reading timeยท4 min
Pass -N
as an argument to the slice()
method to get the last N characters
of a string.
For example, str.slice(-2)
returns the last 2 characters of the string.
const str = 'bobbyhadz.com'; // โ get the last 2 characters of a string const last2 = str.slice(-2); console.log(last2); // ๐๏ธ om // โ get the last 3 characters of a string const last3 = str.slice(-3); console.log(last3); // ๐๏ธ com // โ get the last 4 characters of a string const last4 = str.slice(-4); console.log(last4); // ๐ .com
The String.slice method extracts a section of a string and returns it, without modifying the original string.
The String.slice()
method takes the following arguments:
Name | Description |
---|---|
start index | The index of the first character to include in the returned substring |
end index | The index of the first character to exclude from the returned substring |
When only a single argument is passed to the String.slice()
method, the slice
goes to the end of the string.
Make sure to assign the result of calling the slice()
method to a variable as
the method doesn't change the original string.
The String.slice()
method can be passed negative indexes to count backward.
const str = 'bobbyhadz.com'; console.log(str.slice(-2)); // ๐๏ธ om console.log(str.slice(-5)); // ๐๏ธ z.com
slice
method is the same as specifying a start index of string.length - N
.const str = 'bobbyhadz.com'; const last3 = str.slice(-3); // ๐๏ธ com console.log(last3); const last3Again = str.slice(str.length - 3); console.log(last3Again); // ๐๏ธ com
In both examples, we tell the slice
method to copy the last 3 characters of
the string into a new string.
slice()
returns the entire string and doesn't throw an error.const str = 'bobbyhadz.com'; const last100 = str.slice(-100); console.log(last100); // ๐๏ธ bobbyhadz.com
In this example, we tried to get the last 100
characters of a string that only
contains 13
characters.
As a result, the slice
method returned a copy of the entire string.
If you have to do this often, define a reusable function.
function getLastNchars(str, n) { return str.slice(-n); } const str = 'bobbyhadz.com'; // โ get the last 2 characters of a string console.log(getLastNchars(str, 2)); // ๐๏ธ om // โ get the last 3 characters of a string console.log(getLastNchars(str, 3)); // ๐๏ธ com // โ get the last 4 characters of a string console.log(getLastNchars(str, 4)); // ๐๏ธ .com
The getLastNchars
function takes a string and N as parameters and returns the
last N characters of the string.
String.substring
Alternatively, you can use the String.substring()
method.
For example, str.substring(str.length - 3)
returns the last 3 characters of
the string.
function getLastNchars(str, n) { return str.substring(str.length - n); } const str = 'bobbyhadz.com'; // โ get the last 2 characters of a string console.log(getLastNchars(str, 2)); // ๐๏ธ om // โ get the last 3 characters of a string console.log(getLastNchars(str, 3)); // ๐๏ธ com // โ get the last 4 characters of a string console.log(getLastNchars(str, 4)); // ๐๏ธ .com
The String.substring() method returns a slice of the string from the start index to the excluding end index.
The method takes the following parameters:
Name | Description |
---|---|
start index | The index of the first character to include in the returned substring |
end index | The index of the first character to exclude from the returned substring |
If no end
index is specified the slice goes to the end of the string.
The String.substring()
method doesn't support negative values to count
backward, so we had to calculate the start index based on the string's length.
const str = 'bobbyhadz.com'; // ๐๏ธ om console.log(str.substring(str.length - 2));
slice
and substring
methods are similar, however, you should use the slice
method because its implementation is more intuitive.There are a couple of
differences
between the String.substring()
and the String.slice()
methods:
substring()
method swaps its start and end index if the start index is
greater than the end index. The slice()
method returns an empty string in
this case.const str = 'bobby'; console.log(str.substring(3, 0)); // ๐๏ธ bob console.log(str.slice(3, 0)); // ๐๏ธ ''
substring()
are negative, they are
treated as if they were 0
.const str = 'bobby'; console.log(str.substring(-3)); // ๐๏ธ bobby console.log(str.slice(-3)); // ๐๏ธ bby
When given a negative index, the slice()
method counts backward from the end
of the string to find the indexes.
For these reasons, it is recommended to use the String.slice()
method to get
the last N characters of a string.
The slice
method works in a more predictable manner.
You can learn more about the related topics by checking out the following tutorials: