OneCompiler

[Javascript] How to select last three characters of a given string?

I want to return last three characters of a given string, How to do that in Javascript?

1 Answer

4 years ago by

You can use either substring() or slice() methods to select only last three characters of a given string. Check the usage as shown below:

let str= 'This is a test message'

console.log(str.slice(str.length-3, str.length)); // Outputs age

console.log(str.substring(str.length-3, str.length)); // Outputs age

Run here https://onecompiler.com/javascript/3xnrpnhv7

4 years ago by Meera