How To Get The Last 2 Characters Of A String In JavaScript?

String is a common data type in JavaScript. As a JS dev, you must be familiar with this data type. Today we will teach you many valuable methods when working with string, specifically how to get the last 2 characters of a string in JavaScript.

Get the last 2 characters of a string in JavaScript

There are a bunch of ways to do that. Here we will introduce you three most convenient and fastest ways.

Using charAt() method

Syntax:

charAt(index)

Description:

CharAt() method will return the character at a specific index.

As we know, the index of the last character in the string is length - 1, and the index of the previous of the last character will be length - 2. So, by combining these two indexes with the charAt() function, we will easily get the last two characters of a string in JS.

const str = "learnshareIT";
const lastTwo = str.charAt(str.length - 2) + str.charAt(str.length - 1);
console.log(lastTwo);

Output:

IT

Using slice() method.

Syntax:

slice(start, end)

Parameters:

• start: the index of the starting position on the string you want to extract.
• end (optional): the index of the last character on the string you want to extract.

The slice() method excerpts part of a string and then returns a new string without changing the original string. By default, if without the second argument, slice() will cut to the end of the string.

We can use a trick that is to pass a negative number to the slice(). Based on the last index position being -1, we can easily guess the position of the previous character is -2. See the example below for a better understanding.

const str = "learnshareIT";
const lastTwo = str.slice(-2);
console.log(lastTwo);

Output:

IT

Using the substr() or substring() method.

substr() syntax:

substr(start, length)

Parameters:

• start: index of the starting position on the string you want to extract.
• length: the length of string extracted from an original string.

Description:

The substr() method excerpts characters from start to start + length.

substring() syntax:

substring(start, end)

Parameters:

• start: index of the starting position on the string you want to extract
• end (optional): the index of the last character on the string you want to extract

Description:

The substring() method extracts characters from start to end but not containing the end.

By default, if the 2nd parameter is not passed, both functions will extract to the end of the string. Therefore, we only need to pass the index position near the end to both functions, but the difference is that substr can pass a negative number.

Take a look at the example below:

const str = "learnshareIT";
const lastTwo1 = str.substring(str.length - 2);
const lastTwo2 = str.substr(-2);

console.log("substring: " + lastTwo1);
console.log("substr: " + lastTwo2);

Output:

substring: IT
substr: IT

Summary

Congratulations! you have learned the most straightforward 3 to get the last 2 characters of a string in JavaScript. If you found the article helpful, don’t forget to share it with all your friends. Thank you for reading through this short tutorial!

Maybe you are interested: